XML : Iterate over NodeList of XML in java

  List<String> targetCollection = new ArrayList<>();      try {          XPath xPath = XPathFactory.newInstance().newXPath();          if ( !namespaceContext.isEmpty() ) {              xPath.setNamespaceContext(new MapNamespaceContext(namespaceContext));          }          XPathExpression expression = xPath.compile("Document");            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();          factory.setNamespaceAware(!namespaceContext.isEmpty());          Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));            NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);          for (int i = 0; i < nodeList.getLength(); i++) {              targetCollection.add(nodeToString(nodeList.item(i)));          }      }      catch (SAXException | ParserConfigurationException | XPathExpressionException | IOException e) {          throw new RuntimeException("Cannot split the specified message.");      }    

tried with above code but NOdeList is coming as 0 XML:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  <Document xmlns="test:123:146">      <A>          <B>          </B>          <C>              <D>              <E></E>              </D>          </C>      </A>  </Document>    

(This is a complex xml)While debugging Node List is 0 and document is not showing all the nodes of xml Any suggestion where I am doing wrong?

No comments:

Post a Comment