Read the local xml file and parse it through DOM. The characteristic of DOM parsing is to load the entire xml file into the memory to form a DOM tree structure. The tree structure is convenient for traversal and manipulation.
The characteristic of DOM parsing is to read the xml file and convert it into a dom tree structure, and traverse through the nodes.
This is W3c’s concept of nodes
If the XML contains a large amount of data, DOM is not suitable for parsing XML containing a large amount of data due to the characteristic of loading the XML into the memory at one time. When a large amount of xml is included, parsing with SAX saves memory.
The following is an example of using DOM to parse xml files:
The xml file structure is as follows:
<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis </author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</ author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> < author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
Create a class to parse xml as follows:
package xml.dom;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org .w3c.dom.Node;import org.w3c.dom.NodeList;public class ReadXmlFile { public static void main(String[] args) { try{ File xmlFile = new File("src/resource/book.xml"); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document doc = builder.parse(xmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element: "+doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("book"); for(int i = 0 ; i<nList.getLength();i++){ Node node = nList.item(i); System.out.println("Node name: "+ node.getNodeName()); Element ele = (Element)node; System.out.println("---------------- ------------"); if(node.getNodeType() == Element.ELEMENT_NODE){ System.out.println("book category: "+ ele.getAttribute("category")) ; System.out.println("title name: "+ ele.getElementsByTagName("title").item(0).getTextContent()); System.out.println("author name: "+ele.getElementsByTagName("author").item(0).getTextContent()); System.out.println("year :"+ele.getElementsByTagName("year").item(0).getTextContent()); System.out.println("price : "+ele.getElementsByTagName("price").item(0).getTextContent() ); System.out.println("-------------------------"); } }
Analysis results:
Root element: bookstoreNode name: book--------------------------book category: cookingtitle name: Everyday Italianauthor name: Giada De Laurentiisyear :2005price : 30.00-------------------------Node name: book------------------ ----------book category: childrentitle name: Harry Potterauthor name: J K. Rowlingyear :2005price : 29.99----------------------- --Node name: book--------------------------book category: webtitle name: XQuery Kick Startauthor name: James McGovernyear:2003price: 49.99-------------------------Node name: book------------- ---------------book category: webtitle name: Learning XMLauthor name: Erik T. Rayyear:2003price: 39.95----------------- --------
The above is to obtain the corresponding value through name.
The following is output using loop nodes:
The code for the loop node output mode is as follows:
package xml.dom;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org .w3c.dom.Node;import org.w3c.dom.NodeList; public class ReadXmlFile2 { public static void main(String[] args) { try{ File xmlFile = new File("src/resource/book.xml"); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance() ; DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document doc = builder.parse(xmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element: "+doc.getDocumentElement().getNodeName()); if(doc.hasChildNodes()){ printNode (doc.getChildNodes()); } }catch(Exception e){ e.printStackTrace(); } } public static void printNode(NodeList nodeList){ System.out.println("------------------------"); // System.out.println(nodeList. getLength()); for(int i = 0; i<nodeList.getLength(); i++){ Node node = (Node)nodeList.item(i); if(node.getNodeType() == Node.ELEMENT_NODE){ System.out.println("node name: "+node.getNodeName()); System.out.println("node value: "+node.getTextContent()); if(node.hasAttributes()){ NamedNodeMap nodeMap = node.getAttributes(); for(int j = 0; j < nodeMap.getLength(); j++){ Node nodenew = nodeMap.item(j); System.out.println("node name "+nodenew.getNodeName()); System.out.println("node value "+nodenew.getNodeValue()); } } if(node.hasChildNodes ()){ printNode(node.getChildNodes()); } } } }}
The output is as follows:
Root element: bookstore------------------------node name: bookstorenode value: Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern 2003 49.99 Learning XML Erik T. Ray 2003 39.95 ------------------node name: booknode value: Everyday Italian Giada De Laurentiis 2005 30.00 node name categorynode value cooking------------------------node name: titlenode value: Everyday Italiannode name langnode value en------- ------------------node name: authornode value: Giada De Laurentiis------------------------ node name: yearnode value: 2005------------------------node name: pricenode value: 30.00------------ ------------node name: booknode value: Harry Potter J K. Rowling 2005 29.99 node name categorynode value children------------------------node name: titlenode value: Harry Potternode name langnode value en-------- ----------------node name: authornode value: J K. Rowling-------------------------------- node name: yearnode value: 2005------------------------node name: pricenode value: 29.99------------ ------------node name: booknode value: XQuery Kick Start James McGovern 2003 49.99 node name categorynode value web------------------------node name: titlenode value: XQuery Kick Startnode name langnode value en---------- ---------------node name: authornode value: James McGovern--------------------------node name: yearnode value: 2003--------------------------------node name: pricenode value: 49.99---------------- --------node name: booknode value: Learning XML Erik T. Ray 2003 39.95 node name categorynode value webnode name covernode value paperback------------------------node name: titlenode value: Learning XMLnode name langnode value en---------- -------------node name: authornode value: Erik T. Ray--------------------------node name: yearnode value: 2003------------------------node name: pricenode value: 39.95--------------- ----------
Questions about nodes:
<book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </ book>
For book applications: doc.getChildNodes() gets a NodeList where the length of the NodeList is 9
The 9 nodes are as follows:
title node
lang node
Everyday node
author node
Giada De Laurentiis node
year node
2005 node
price node
30.00 node