0

My Xml File looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pointList SYSTEM "point.dtd">
<pointList>
<point unit="mm">
<x>2</x>
<y>3</y>
</point>

<point unit="cm">
<x>9</x>
<y>3</y>
</point>

<point unit="px">
<x>4</x>
<y>7</y>
</point>

</pointList>

When i try to get attributes of tag point:

import java.io.File;
import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TryXml {
    public static void main (String [] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
        }
        catch (ParserConfigurationException e){
            System.out.println(e.getMessage());
        }
        File f = new File("p1.xml");
        try {
            doc=builder.parse(f);
        }
        catch (SAXException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        Element root = doc.getDocumentElement();
        System.out.println(root.getTagName());

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


        NodeList nList = root.getChildNodes();
        for (int i=0;i<nList.getLength();i++) {
        if(nList.item(i) instanceof Element)

        System.out.println(nList.item(i).getAttributes());
    }

    }
    }

all i get is something like the address:

com.sun.org.apache.xerces.internal.dom.AttributeMap@3214512e
com.sun.org.apache.xerces.internal.dom.AttributeMap@53ddbcb1
com.sun.org.apache.xerces.internal.dom.AttributeMap@28f337b

can anyone give me a hint on how to get the attributes of point and maybe other inner tags?

Gipsy
  • 265
  • 3
  • 6
  • 18

4 Answers4

1

You could replace your println with the following:

System.out.println(((Element) nList.item(i)).getAttribute("unit"));

This should give you the "unit" attribute of the current element.

Oliver
  • 522
  • 3
  • 11
  • But if i use "unit" how do i get the values of inner tags? i need the result to look like this: `point 23mm, point 93cm, point 47px` – Gipsy May 03 '12 at 12:45
  • 1
    You now have the `Element` and can from there work your way through the childs: `((Element) nList.item(i)).getChildNodes()` will give you another `NodeList` through which you can iterate exactly as you did through `nList` in your code. – Oliver May 03 '12 at 12:49
0

Use,

"Element root = doc.getElementByTagName("pointList"); "

instead of,

"Element root = doc.getDocumentElement(); "

Phalgun D
  • 21
  • 1
0

Element.getAttributes() will give a list of all attributes in that Element. If you know beforehand the name of the attribute and just want to get its value, use Element.getAttribute(String) instead.

If you need to get the child elements, use Element.getChildNodes(). To get the text inside an Element, or more specifically a Node, use getNodeValue().

For instance:

        NodeList nList = root.getChildNodes();
        String out = "";
        for (int i=0;i<nList.getLength();i++) {
            // Assuming all first-level tags are <point>
            Element point = (Element) nList.item(i);
            String unit = point.getAttribute("unit");
            out += "point ";
            for (int y=0;y<point.getChildNodes().getLength();y++){
                Node child = point.getChildNodes().item(y);
                // String nodeName = child.getNodeName();
                String nodeValue = child.getNodeValue();
                out += nodeValue;
            }
        out += unit;
        out += ", ";
        }

Will output point 23mm, point 93cm, point 47px,.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • What do i put in System.out.println()?? and where in the code should i place it? and also ` Element point = (Element) nList.item(i); ` says that cannot be cast to org.w3c.dom.Element – Gipsy May 03 '12 at 20:10
0

this post getting xml attribute with java does what you are trying to achieve. It teaches regular xml parsing and manipulation. It also retrieves attribute.

Good luck!

Community
  • 1
  • 1
pelumi
  • 1,530
  • 12
  • 21