[SOLVED] XPath to select multiple child values


How can i select all/multiple the values which satify below condition




        <X2 code="a">
        <X2 code="b">
        <X2 code="b">



import org.dom4j.Document;
import org.dom4j.Node;
Document doc = reader.read(new StringReader(xml));
Node valueNode = doc.selectSingleNode(XPATH);

Expected value

x3b11, X3b12


Instead of Document.selectSingleNode(), use Document.selectNodes() to select multiple nodes.

Also consider XPath.selectNodes(); here is a full example from the DOM4J Cookbook:

import java.util.Iterator;

import org.dom4j.Documet;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;

public class DeployFileLoaderSample {

  private org.dom4j.Document doc;
  private org.dom4j.Element root;

  public void browseRootChildren() {

      Let's look how many "James" are in our XML Document an iterate them  
      ( Yes, there are three James in this project ;) )
    XPath xpathSelector = DocumentHelper.createXPath("/people/person[@name='James']");
    List results = xpathSelector.selectNodes(doc);
    for ( Iterator iter = results.iterator(); iter.hasNext(); ) {
      Element element = (Element) iter.next();

    // select all children of address element having person element with attribute and value "Toby" as parent
    String address = doc.valueOf( "//person[@name='Toby']/address" );

    // Bob's hobby
    String hobby = doc.valueOf( "//person[@name='Bob']/hobby/@name" );

    // the second person living in UK
    String name = doc.value( "/people[@country='UK']/person[2]" );
    // select people elements which have location attriute with the value "London"
    Number count = doc.numberValueOf( "//people[@location='London']" );

Answered By – kjhughes

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.