要搜索按
XML 搜索索引规则建立索引的内容,请以格式 elementName[attributeName='attributeValue']
定义搜索文本。将此搜索文本作为属性查询字段添加在
RAMAssetQueryBuilder 上。可以通过产生的
ArtifactSearchResults 来检索在特定
XML 匹配项下的结果。用于在 XML 数据内进行搜索的示例格式。
- 不带元素的搜索:attributeName='attributeValue'
- 用于查找在单个元素中定义的多个属性的搜索:elementName[attributeName1='attributeValue1'
attributeName2='attributeValue2]
- 用于查找完整路径的搜索:elementPath1/elementPath2[attributeName='attributeValue']
- 用于查找元素中的文本的搜索:elementName[text()='textValue']
- 当搜索 XML 文档时,还可使用通配符:elementName[attributeName='attribute*']
//Perform search into XML indexed content
RAMAssetQueryBuilder query = new RAMAssetQueryBuilder(session);
//Make sure search within artifacts is on
query.setSearchModes(SearchQuery.SEARCH_WITHIN_ARTIFACTS_FLAG);
//XML index search text in the form elementName[attributeName='attributeValue']
String searchText = "wsdl:definitions[targetNamespace='*example*']";
query.addQueryField(query.QUERY_FIELD_ATTRIBUTE, searchText);
//Perform search
SearchResult result = session.getAssets(query);
//Get the list of asset matches
AssetSearchResult[] results = result.getAssetSearchResults();
for(int i = 0; i < results.length; i++){
//For an asset match get the list of artifact matches
ArtifactSearchResult[] artifactMatches = results[i].getMatchingArtifacts();
for(int j = 0; j < artifactMatches.length; j++){
//For an artifact match get the XML index matches
String[] xmlIndexMatches = artifactMatches[j].getMatches();
for(int k = 0; k < xmlIndexMatches.length; k++){
System.out.println("Match = " + xmlIndexMatches[k]);
}
}
}