0

I am trying to parse an xml file and would like to get only certain information.

...
<field name="firstname">
    <value>John</value>
</field>
<field name="lastname">
    <value>Citizen</value>
</field>
<field name="DoB">
    <value>01/01/1980</value>
</field>
<field name="Profession">
    <value>"Manager"</value>
</field>
....

and I have document and now I can print out all the values by looping the NodeList of tag, however, I don't know how to get the specific value, e.g. I want only the last name and the Profession from the list, so my goal is I want to have: "Citizen" and "Manager"

Any clue please.

thank you

Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47
  • did you look for a DOM tutorial? i'm sure there are a few online. – jtahlborn Feb 26 '14 at 13:07
  • yes you can be sure there are some and I have gone through some but most of them are playing with tags without the attributes, e.g. name like the example above – Dilshad Abduwali Feb 26 '14 at 13:10
  • You can try it through recursion - one of my detailed example [here](http://stackoverflow.com/questions/21534340/using-java-to-parse-xml/21713344#21713344) – StoopidDonut Feb 26 '14 at 13:58

2 Answers2

1

Experiment with the DOM API (play a bit with the methods of Document, Element and Node) and you will be comfortable manipulating XML very quickly.

In the example below, I loaded your XML as a string, extracted the nodes and placed them in a Map. You can use it as a starting point to write your code. I just added a <root> to your XML so I could place the code in a working example.

public class DOMExampleStackOverflow3 {

    public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, TransformerException {

        InputSource source = new InputSource(new StringReader("<root>\n" +
                                                            "<field name=\"firstname\">\n" +
                                                            "    <value>John</value>\n" +
                                                            "</field>\n" +
                                                            "<field name=\"lastname\">\n" +
                                                            "    <value>Citizen</value>\n" +
                                                            "</field>\n" +
                                                            "<field name=\"DoB\">\n" +
                                                            "    <value>01/01/1980</value>\n" +
                                                            "</field>\n" +
                                                            "<field name=\"Profession\">\n" +
                                                            "    <value>Manager</value>\n" +
                                                            "</field>\n" +
                                                            "</root>" ));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        Document document = documentBuilder.parse(source);

        NodeList allFields = (NodeList) document.getElementsByTagName("field");

        Map<String, String> data = new HashMap<>();
        for(int i = 0; i < allFields.getLength(); i++) {
            Element field = (Element)allFields.item(i);
            String nameAttribute = field.getAttribute("name");
            Element child = (Element)field.getElementsByTagName("value").item(0);
            String value = child.getTextContent();
            data.put(nameAttribute, value);
        }

        for(Map.Entry field : data.entrySet()) {
            System.out.println(field.getKey() + ": " + field.getValue());
        }
    }
}

The result is:

firstname: John
Profession: Manager
DoB: 01/01/1980
lastname: Citizen

Now you can alter the code and use the attribute information in a conditional block extract just what you need.

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
-1
Try This......


package com.mkyong.seo;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {

    try {

    File fXmlFile = new File("/Users/mkyong/staff.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

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

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

    System.out.println("----------------------------");

    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("Staff id : " + eElement.getAttribute("id"));
            System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
  }

}




For More Reference: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
user3283976
  • 179
  • 4
  • I've seen this one already, but after you've got the NodeList, how do you differentiate the tags, I need to check the if the tag has the attribute name = "Profession" – Dilshad Abduwali Feb 26 '14 at 13:14
  • 1
    eElement.getElementsByTagName("field").item(0).getnodeName() it will return your field name then u can put if condition for which names you want like if(eElement.getElementsByTagName("field").item(0).getnodeName().equals("Profession")) – user3283976 Feb 26 '14 at 13:25
  • 1
    just copy & paste of code from other sites are discouraged here. – sakura Feb 26 '14 at 13:34