简体   繁体   中英

Read and print out XML file to console

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>

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM