0

I'm new to Java and need some help. I have a XML that looks like:

String pXML =
"<root>
     <x>1</x>
     <x>2</x>
     <x>3</x>
     <x>4</x>
 </root>"

And I would like to get a List object that contains all of the values inside x tag.

I've tried with javax.xml.parsers.DocumentBuilderFactory:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder();
document = (Document) builder.parse( new InputSource( new StringReader(pXML) ) );
Node n = document.getFirstChild();
NodeList n1 = n.getChildNodes();
//and then I go through all the nodes and insert the values into a list

But this doesn't contain the x nodes.

2
  • 1
    I suspect the quote marks are causing the parser to skip all of the XML. Commented Mar 26, 2013 at 13:47
  • No, the pXML object I get as an input parametar, this is for you to understand what the xml looks like... Commented Mar 26, 2013 at 13:57

3 Answers 3

3

You can use XPath to get the values of all the x nodes as follows:

public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException, XPathExpressionException {
    final String pXML = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>";
    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(pXML.getBytes()));
    final XPathExpression xPathExpression = XPathFactory.newInstance().newXPath().compile("//x/text()");
    final NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
    final List<String> values = new LinkedList<>();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        values.add(nodeList.item(i).getNodeValue());
    }
    System.out.println(values);
}

Output:

[1, 2, 3, 4]

This has the advantage of being a very general solution, easily adaptable if the structure of the XML changes.

It also has the advantage of being, in my opinion, much more understandable than iterating over the nodes in the Document by hand.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed and using xPath is much less error prone. Iterating child nodes of an element may give you a blend a Text nodes and (sub) Elements that you would not necessarily expect.
Figured out what was the problem - in my code NodeValues in NodeList had non-trimmed values, so it had tons of values, from '\n' and ' '. Just a mess of chars and strings...
0

Try Node n = document.getDocumentElement(); to recover the root element of your XML

Comments

0

Try This

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XML {
  public static void main(String arg[]) throws Exception{
    String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>";

    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);
    NodeList nodes = doc.getElementsByTagName("x");
    System.out.println(nodes.getLength());
    List<String> valueList = new ArrayList<String>();
    for (int i = 0; i < nodes.getLength(); i++) {
      Element element = (Element) nodes.item(i);

      String name = element.getTextContent();

     // Element line = (Element) name.item(0);
      System.out.println("Name: " + name);
      valueList.add(name);
    }

  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.