我试图解析下面的xml,但是在每次迭代时重复第一个子标记[]而不是获取下一个子标记的值[即]。请提供帮助。
<error_code_rules enabled="true">
<errors>
<error>
<error_source>ldap</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-1</oms_error_code>
<priority>3</priority>
</error>
<error>
<error_source>nagravision</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-2</oms_error_code>
<priority>1</priority>
</error>
<error>
<error_source>hitexpress</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-3</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>netinventory</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-4</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>seachangeeventis</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-5</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>embratel</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-6</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>siemens</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-7</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>netsiemens</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-8</oms_error_code>
<priority>2</priority>
</error>
<error>
<error_source>nokiaonends</error_source>
<error_code>ALL</error_code>
<oms_error_code>OMS-9</oms_error_code>
<priority>2</priority>
</error>
</errors>
</error_code_rules>
解析xml doc的代码如下
private void populateKsuRulesList() {
logger.trace(3, "Populating ksuRulesList");
logger.log("populter ksu rule list");
CachedXPathAPI xPathAPI = new CachedXPathAPI();
try {
NodeList components = xPathAPI.eval(doc, KSU_RULES_COMPONENT_PATH)
.nodelist();
logger.log("components.getLength()" + components.getLength());
for (int i = 0; i < components.getLength(); i++) {
Node component = components.item(i);
// NodeIterator nodeItr = xPathAPI.selectNodeIterator(component,
// KSU_RULES_COMPONENT_CATEGORY_PATH);
String componentCategory = xPathAPI.eval(components.item(i),
KSU_RULES_COMPONENT_CATEGORY_PATH).toString();
String componentType = xPathAPI.eval(component,
KSU_RULES_COMPONENT_TYPE_PATH).toString();
String mandatory = xPathAPI.eval(component,
KSU_RULES_MANDATORY_PATH).toString();
logger.log("componentCategory" + componentCategory);
logger.log("componentType" + componentType);
logger.log("mandatory" + mandatory);
String ruleParms[] = { componentCategory, componentType,
mandatory };
ksuRulesList.add(ruleParms);
}
} catch (TransformerException te) {
logger.log("Exception: ", te);
}
}
答案 0 :(得分:0)
试试这个。
NodeList components = xPathAPI.eval(doc, "//errors/error").nodelist();
System.out.println("components.getLength()" + components.getLength());
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
for (int i = 0; i < components.getLength(); i++) {
Node component = components.item(i);
Element product = (Element) component;
NodeList nodes = (NodeList) xpath.compile("error_source").evaluate(product, XPathConstants.NODESET);
System.out.println(nodes.item(0).getTextContent());
}