M
M
Mikhail Konyukhov2012-06-17 19:24:26
Java
Mikhail Konyukhov, 2012-06-17 19:24:26

I parse xml into Java, I get null. How to do it right?

I parse this xml:

<?xml version="1.0"?>
<store>
<object>
        <id>1</id>
        <counter>2</counter>
        <isCommon>false</isCommon>
        <raw>А</raw>
        <childs>
        </childs>
        <types>
        </types>
</object>
</store>


I do it like this:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
Document doc;
try {
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  doc = dBuilder.parse(new File(xmlfile));
}catch(Exception e){
  System.out.println(e.getMessage());
  System.exit(1);
  return;
}
NodeList nList = doc.getElementsByTagName("object");
Node ObjectItem = nList.item(0);
Element element = (Element)ObjectItem;
Sytem.out.println(element.getElementsByTagName("counter").item(0).getNodeValue());


On the output I get null, although there is not null. how to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Moxa, 2012-06-17
@piromanlynx

System.out.println(element.getElementsByTagName("counter").item(0).getTextContent());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question