https://www.w3schools.com/xml/dom_nodes.asp
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class DomParserDemo {
public static void main(String argv[]) {
try {
// File fXmlFile = new File("staff.xml");
File fXmlFile = new File("book.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
// NodeList nList = doc.getElementsByTagName("staff");
NodeList nList = doc.getElementsByTagName("book");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// System.out.println("Staff id : " + eElement.getAttribute("id"));
// System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
// System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
// System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
// System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
System.out.println("category : " + eElement.getAttribute("category"));
System.out.println("title : " + eElement.getElementsByTagName("title").item(0).getTextContent());
System.out.println("author : " + eElement.getElementsByTagName("author").item(0).getTextContent());
System.out.println("year : " + eElement.getElementsByTagName("year").item(0).getTextContent());
System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT :
Root element :bookstore
Current Element :book
category : cooking
title : Everyday Italian
author : Giada De Laurentiis
year : 2005
price : 30.00
Current Element :book
category : children
title : Harry Potter
author : J K. Rowling
year : 2005
price : 29.99
Current Element :book
category : web
title : XQuery Kick Start
author : James McGovern
year : 2003
price : 49.99
Current Element :book
category : web
title : Learning XML
author : Erik T. Ray
year : 2003
price : 39.95
Text is Always Stored in Text Nodes
A common error in DOM processing is to expect an element node to contain text.
However, the text of an element node is stored in a text node.
In this example: 2005, the element node holds a text node with the value “2005”.
“2005” is not the value of the element!
DOM Example
Look at the following XML file (books.xml):
The root node in the XML above is named .
All other nodes in the document are contained within .
The root node holds 4 nodes.
The first node holds the child nodes:
The child nodes contain one text node each, “Everyday Italian”, “Giada De Laurentiis”, “2005”, and “30.00”.
Text is Always Stored in Text Nodes
A common error in DOM processing is to expect an element node to contain text.
However, the text of an element node is stored in a text node.
In this example: 2005, the element node holds a text node with the value “2005”.
“2005” is not the value of the element!
The XML DOM Node Tree
The XML DOM views an XML document as a tree-structure. The tree structure is called a node-tree.
All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.
The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree: