1

I need to printout some attributes from an xml file i've created.

So far i am inspired by MKyoung and have made some changes in his. But i cant get the xml attributes. I want to print out the name in the "" and the score.

    <score name="Rasmus" score="10000"/>

My code for getting it is:

public void readXMLFile() {
    try {

        File fXmlFile = new File("highscore.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("Highscore");

        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("Name : " + eElement.getAttribute("score"));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And my xml file named highscore.xml contains this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Highscore>
  <score name="Rasmus" score="10000"/>
  <score name="Søren" score="6000"/>
  <score name="Niclas" score="5000"/>
</Highscore>
4levels
  • 3,134
  • 1
  • 23
  • 22
Codeboy
  • 35
  • 1
  • 11
  • 1
    you have to iterate through the next level of nodes. Please check [my answer](https://stackoverflow.com/a/47248264/5832518) – Raju Nov 12 '17 at 11:57

2 Answers2

2

I have edited your code by adding two lines. Check below. Basically, you have to iterate through next level of nodes.

code:

public void readXMLFile() {
    try {

        File fXmlFile = new File("highscore.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        System.out.println("Root element : " + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("Highscore");
        Node child = nList.item(0);
        NodeList nL = child.getChildNodes();    
        System.out.println("----------------------------");
        int i = 1;
        for (int temp = 0; temp < nL.getLength(); temp++) {
            Node nNode = nL.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println(i + "," + eElement.getAttribute("name") + "," + eElement.getAttribute("score"));
                i++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Output:

Root element : Highscore
----------------------------
1,Rasmus,10000
2,Søren,6000
3,Niclas,5000
4levels
  • 3,134
  • 1
  • 23
  • 22
Raju
  • 2,902
  • 8
  • 34
  • 57
  • Ye exactly like this! And if i only want to count on the scores and not the #text? Im trying to make a highscore list count so it says 1. "Name" "Score" and next is 2. "Name" "Score"? Cause if i use the counter temp then it counts the #text also – Codeboy Nov 12 '17 at 12:48
  • @NiclasJohansen, Please check the updated answer and output. I have used `i` as the counter. – Raju Nov 12 '17 at 13:24
  • Thank u very much! – Codeboy Nov 12 '17 at 13:37
1

YOu can use open source libaries like simple-xml.

private Serializer serializer = new Persister();

// Reading
(YOUROBJECT) serializer.read(YOUROBJECT.class, new File(path));
// Writing
serializer.write(YOUROBJECT, file);
Shineed Basheer
  • 578
  • 5
  • 16