user2528595
user2528595

Reputation: 75

Java XML Reading

I've been wondering how to read XML files, but before you answer, read the whole post.

For example I have:

<?xml version="1.0" encoding="UTF-8"?>

<messages>

<incoming id="0" class="HelloIlikeyou" />

</messages>

What I want, is get all values from the tag . I want to place it in a dictionary, which key is incoming/outgoing, and then it will contain a list of Pair as value, with as key the id value and as value the class value.

So I got this:

HashMap<String, List<Pair<Integer, String>>> headers = new HashMap<>();

Then it will store this:

HashMap.get("incoming").add(new Pair<>("0", "HelloIlikeyou"));

But I don't know how to do it, I already got a part but it aint working:

File xml = new File(file);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xml);
        doc.getDocumentElement().normalize();

        NodeList nodes = doc.getElementsByTagName("messages");

        for (int i = 0; i < nodes.getLength(); i++) {

            Node node = nodes.item(i);

                System.out.println("Type: " + node.getNodeValue() + " packet ID " + node.getUserData("id"));    
            }

Upvotes: 3

Views: 1445

Answers (4)

Nikola Yovchev
Nikola Yovchev

Reputation: 10206

Use one of the many available libraries that will do that for you, for example XStream:

http://x-stream.github.io/

Upvotes: 0

Manuel Manhart
Manuel Manhart

Reputation: 5455

This is what you want:

    public static void main(final String[] args)
    throws ParserConfigurationException, SAXException, IOException {
File xml = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
doc.getDocumentElement().normalize();

NodeList nodes = doc.getElementsByTagName("messages");

for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    for (int j = 0; j < node.getChildNodes().getLength(); j++) {

    Node child = node.getChildNodes().item(j);

    if (!child.getNodeName().equals("#text")) {
        NamedNodeMap attributes = child.getAttributes();

        System.out.println("Type: " + child.getNodeName()
            + " packet ID " + attributes.getNamedItem("id")
            + " - class: " + attributes.getNamedItem("class"));
    }
    }
}
}

This gives me the following output:

Type: incoming packet ID id="0" - class: class="HelloIlikeyou"

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109532

Node node = nodes.item(i);
if (node instanceOf Element) {
    Element elem = (Element)node;
    String id = elem.getAttribute("id");
    ...

So you were almost there. The W3C classes are a bit old-stylish.

Upvotes: 0

Alks
Alks

Reputation: 215

You can use JAXB, i think that is the best way. take a look of this: Jaxb tutorial

Upvotes: 3

Related Questions