3

I'm trying to get the numbers from the String tags only. Right now, the Java program thinks the string tags contain null. Thanks for the help in advance!

Here is the XML

<?xml version="1.0"?>
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>6540321</string>
  <string>6540322</string>
  <string>6540323</string>
  <string>6540324</string>
  <string>6540325</string>
</ArrayOfstring>

Here is what I've tried

public static void toOrderListFromXML() throws ParserConfigurationException, SAXException, IOException {
    File fXmlFile = new File("test.xml");

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("string");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;

            System.out.println("Order number: " + eElement.getNodeValue());
        }
    }
}

Output:

Root element :ArrayOfstring

Current Element :string
Order number: null

Current Element :string
Order number: null

Current Element :string
Order number: null
.......................
.......................

Here is the tutorial I attempted to follow: http://www.java2s.com/Code/Java/XML/ParseanXMLstringUsingDOMandaStringReader.htm

Stephen-Wisniewski
  • 321
  • 1
  • 2
  • 13

2 Answers2

2
public static void toOrderListFromXML() throws ParserConfigurationException, 
    SAXException, IOException {
        File fXmlFile = new File("test.xml");

        DocumentBuilderFactory dbFactory = 
   DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + 
   doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("string");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;

            System.out.println("Order number: " + 
          eElement.getTextContent());
         }
     }
   }
 }

Then output will be

Root element :ArrayOfstring

Current Element :string
Order number: 6540321

Current Element :string
Order number: 6540322

Current Element :string
Order number: 6540323

Current Element :string
Order number: 6540324

Current Element :string
Order number: 6540325
Log Raj Bhatt
  • 167
  • 2
  • 10
1

You could try to use .getTextContent()

also see: Getting XML Node text value with Java DOM

Bender
  • 617
  • 6
  • 17