简体   繁体   中英

trying to get nodeList through getting childnode value

Okay so I have this xml:

<employees>
   <employee>
     <name>John</name>
     <surname>Smith</surname>
     <age>18</age>
   </employee>
  <employee>
     <name>Test</name>
     <surname>1</surname>
     <age>18</age>
  </employee>
  <employee>
     <name>Cat</name>
     <surname>Dog</surname>
     <age>18</age>
   </employee>
   <employee>
     <name>John</name>
     <surname>Bravo</surname>
     <age>19</age>
   </employee>
</employees>

I am using Xpath to get the child node:

File f = new File("employees.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder =  builderFactory.newDocumentBuilder();
Document doc = builder.parse(f);
XPath xPath =  XPathFactory.newInstance().newXPath();

String val = "John";

String expression = "./employees[employee/name='"+ val +"']";
Node locatedNode = (Node) xPath.evaluate(expression, doc, XPathConstants.NODE);
Nodelist result = locatedNode.getParentNode().getChildNodes();

After that I use a for loop to loop through the nodelist to obtain surname of employees. In this case, I will get the two John's surname and add it to an array list

    List<Surname> sname = new ArrayList<Surname>;

for (int i = 0; i < result.getLength(); i++) {
    Node nNode = result.item(i);

     if (nNode.getNodeType() == Node.ELEMENT_NODE) {
         Element eElement = (Element) nNode; 
         String surname = eElement.getElementsByTagName("surname").item(0).getTextContent();
         sname.add(new Surname(surname));
     }
}

The problem is i kept on unable to get the Node, it kept throwing nullpointexception error. Is it that my expression is wrong or this line:

Nodelist result = locatedNode.getParentNode().getChildNodes();

is wrong?

What I wanted to get (surname & age) based on surnamed(John): Smith 18 Bravo 19

Surnames of both John employee

These lines do the job of finding surname nodes:

String expression = "./employees/employee[name='"+ val +"']/surname";
NodeList result = (NodeList) xPath.evaluate(expression, doc, XPathConstants.NODESET);

Then you can extract text values, eg

for (int i = 0; i < result.getLength(); i++)
{  
  Node node = result.item(i);
  System.out.println(node.getTextContent() ); 
} // end for i

UPD. If there are several subnodes of interest, then XPath evaluation can be done in two steps: first at the document level, then at a node level:

String expression = "./employees/employee[name='"+ val +"']";
NodeList elements = (NodeList) xPath.evaluate(expression, doc, XPathConstants.NODESET);
for (int i = 0; i < elements.getLength(); i++)
{  
  Node node = elements.item(i);
  Node surname = (Node) xPath.evaluate("surname", node, XPathConstants.NODE);
  Node age = (Node) xPath.evaluate("age", node, XPathConstants.NODE);
  System.out.println(surname.getTextContent() + " " + age.getTextContent() );
} // end for i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM