XML child node attribute value
Posted
by c0mrade
on Stack Overflow
See other posts from Stack Overflow
or by c0mrade
Published on 2010-05-14T10:06:43Z
Indexed on
2010/05/14
10:14 UTC
Read the original article
Hit count: 344
I'm trying to read xml file, ex :
<entry>
<title>FEED TITLE</title>
<id>5467sdad98787ad3149878sasda</id>
<tempi type="application/xml">
<conento xmlns="http://mydomainname.com/xsd/radiofeed.xsd" madeIn="USA" />
</tempi>
</entry>
Here is the code I have so far :
public void parseXML(String xml) {
try {
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("entry");
System.out.println("Information of all entries");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList title = fstElmnt.getElementsByTagName("title").item(0).getChildNodes();
System.out.println("Title : " + ((Node) title.item(0)).getNodeValue());
NodeList id = fstElmnt.getElementsByTagName("id").item(0).getChildNodes();
System.out.println("Id: " + ((Node) update.item(0)).getNodeValue());
NodeList contento= fstElmnt.getElementsByTagName("tempi").item(0).getChildNodes();
System.out.println("Contento : " + ((Node) content.item(0)).getFirstChild().getAttributes());
// with this line above I'm having problems, I get null pointer exception
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
How can I read/get, contento tag attributes? I'm using org.w3c.dom lib.
© Stack Overflow or respective owner