2014-01-20 6 views
0

Я пытаюсь проанализировать следующий xml, но получаю повторный первый субтег [] на каждой итерации вместо получения значений следующих подтеков [i.e]. Просьба оказать вам помощь.Проблема при анализе xml с использованием xPathAPI

<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-документ выглядит следующим образом

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

Вы забыли показать нам свои строки XPath. – vanje

ответ

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()); 
     } 
Смежные вопросы