1

I have this xml file

<Cdtr>
    <Nm>DEF Electronics</Nm>
    <PstlAdr>
        <AdrLine>Corn Exchange 5th Floor</AdrLine>
        <AdrLine>Mark Lane 55</AdrLine>
        <AdrLine>EC3R7NE London</AdrLine>
        <AdrLine>GB</AdrLine>
    </PstlAdr>
</Cdtr>

I am trying to parse the xml, get leaf tags names(which has values) and their respective values using dom parser in Java. Following is the code i am using for that.

public class GetNodeValues {

  public static void main(String[] args) {

    try {

   String xmlFile = "C:/Users/Administrator/workspace/sample.xml";
   File file = new File(xmlFile);
   if(file.exists()){
   // Create a factory
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   // Use the factory to create a builder
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(xmlFile);
   doc.getDocumentElement().normalize();
   // Get a list of all elements in the document
   NodeList list = doc.getElementsByTagName("*");
   System.out.println("XML Elements: ");


   for (int i=0; i<list.getLength(); i++) {
     // Get element
    Element element = (Element)list.item(i);
    String nodnam = element.getNodeName();
    NodeList nl = doc.getElementsByTagName(nodnam);
    Element ele = (Element) nl.item(0);
    if (ele.getChildNodes().getLength() > 0) // then it has text

          String val = ele.getChildNodes().item(0).getNodeValue();
          if (val.startsWith("\n")) {         //Discarding pseudo nodes
          }else {
        System.out.println("Node: "+nodnam+" | Val: "+val); //print node names and values
          }
         }
   }
}
else{
   System.out.print("File not found!");
}
}
catch (Exception e) {
  System.exit(1);
   }
  }
}

I am getting the following result.

Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Corn Exchange 5th Floor

Please help. I am not understanding why its repeating the tag value. The expected output is

Node: AdrLine | Val: Corn Exchange 5th Floor
Node: AdrLine | Val: Mark Lane 55
Node: AdrLine | Val: EC3R7NE London
Node: AdrLine | Val: GB

2 Answers 2

2

While parsing the document, you use Document.getElementsByTagName(String tagname). According to its Javadoc, it returns all Elements in a document in document order. Since your structure has multiple AddrLine elements, and since you always pick the zeroth element from this list, it always gives you the same element.

Instead, you might want to write something like this:

for (int i = 0; i < list.getLength(); i++) {
    Element element = (Element) list.item(i);
    String nodnam = element.getNodeName();

    if (element.getChildNodes().getLength() > 0) // then it has text
    {
    // etc., etc.

Summarising, don't try to retrieve an element from the document when you already have it available.

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

5 Comments

Hi thanks for the quick reply. Sorry but after changing 0 to i the output is coming as blank. I mean its not outputing anything.
I've updated the answer, and also tried to explain why it didn't work.
Thanks a lot mthmulders, the program is working fine now. I did the changes as you suggested...Thanks again :)
Excellent, worked for me on a totally different xml issue. Much appreciated!
That is good work @mthmulders, you got it right.. just to clarify, I've posted the actual code but i don't need any votes.
0

Here is the program recompiled to print all the elements in an xml document

    public static void main(String[] args) {

    try {

        String xmlFile = "staff.xml";
        File file = new File(xmlFile);
        if (file.exists()) {
            // Create a factory
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            // Use the factory to create a builder
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            // Get a list of all elements in the document
            NodeList list = doc.getElementsByTagName("*");

            for (int i = 0; i < list.getLength(); i++) {
                // Get element
                Element element = (Element) list.item(i);
                String nodnam = element.getNodeName();
                if (element.getChildNodes().getLength() > 0) {
                    String val = element.getChildNodes().item(0)
                            .getNodeValue();
                    if (!val.startsWith("\n"))
                        System.out.println(nodnam + "==" + val);
                }
            }
        } else {
            System.out.print("File not found!");
        }
    } catch (Exception e) {
        System.exit(1);
    }
}

There you go. It prints all the elements in the file.

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.