File fXmlFile = new File(path);//path is a String
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document XMLbook = dBuilder.parse(fXmlFile);
Node root = XMLbook.getFirstChild(); //(1)
Node book = XMLbook.getElementsByTagName("BOOK").item(0);//(2)
Node lastChapter=book.getLastChild();//(3)
Node lastSubChapter=lastChapter.getLastChild(); //(4)
...
}
The problem is it reaches null at lastSubChapter
so my program ends bad :( and i have no idea what i did wrong...the code seems okay.
Also, i don`t get what NULL represents: the node value?
- [ROOT: NULL ] - what i see when debugging
- [ BOOK: NULL]- what i see when debugging
- [ #text: ] - what i see when debugging - what does it mean?
- PROBLEM!! here
lastSubChapter
gets NULL :( why?
The XML file I am reading from is:
<?xml version="1.0"?>
<ROOT>
<TITLE>Portocalia ca zapada</TITLE>
<BOOK>
<Chapter name="1">
<Subchapter name="1.1">
<paragraph>Primul paragraf!</paragraph>
</Subchapter>
</Chapter>
<Chapter name="2">
<Subchapter name="2.1">
<paragraph>A fost o data ca niciodata</paragraph>
</Subchapter>
<Subchapter name="2.2">
<paragraph>In luna a13a ea s-a maritat.</paragraph>
</Subchapter>
<Subchapter name="2.3">
<paragraph>In continuare, bineinteles</paragraph>
<paragraph>asa cum se intampla in toate povestile</paragraph>
</Subchapter>
</Chapter>
</BOOK>
</ROOT>
I want to get the last node Subchapter
, the one whose name is "2.3".
Is there another safer way i can get it?