1 There are four ways to parse XML files in Java
- A. Parse XML data using DOM method
The tree structure helps to better understand and master, and the code is easy to write. During the parsing process, the tree structure is saved in the memory, making it easy to modify.
- B. SAX analysis
Using event-driven mode, the memory consumption is relatively small, suitable for use when only processing data in xml
- C. JDOM mode analysis
Extensive use of the Collections class
- D. DOM4J analysis
An intelligent branch of JDOM that incorporates many functions beyond basic XML document representation; it has
superior performance, flexibility, powerful functions, and extreme ease of use.
2 XML files to be processed
<?xml version="1.0" encoding="UTF-8"?> <books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book id="1001"> <name>JAVA 高级编程</name> <author>张三</author> <price>85.72</price> </book> <book id="1002"> <name>C++和C#</name> <author>李失失</author> <price>125.73</price> </book> </books>
3 Steps to parse XML data using DOM
a. Create a DocumentBuilderFactory object
b. Create a DocumentBuilder object
c. Get the Document object through the parse() method of DocumentBuilder
d. Get the list of nodes through the getElementsByTagName() method
e. Use a for loop to traverse the node
f. Get the values of all nodes Attributes and attribute values
g. Get the node names and node values of all nodes
import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom. NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class TestDom4Xml { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //Create a DocumentBuilderFactory object DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //Create a DocumentBuilder object DocumentBuilder db = dbf.newDocumentBuilder(); //Get the Document object through the parse() method of DocumentBuilder Document doc = db.parse("book.xml"); //Get the list of nodes through the getElementsByTagName() method NodeList nodelist = doc.getElementsByTagName("book"); System.out.println(nodelist.getLength()) ; //Use a for loop to traverse nodes for(int i=0;i<nodelist.getLength();i++){ Node node = nodelist.item(i); //Get the objects of all node attributes and attribute values NamedNodeMap nnm = node.getAttributes(); for(int j=0;j<nnm.getLength();j++){ Node sub_node = nnm.item(j); //Get the attributes and attribute values of all nodes System.out.println(sub_node.getNodeName() + " : " + sub_node.getNodeValue()); } //Get all nodes The child node NodeList childlist = node.getChildNodes(); for(int j=0;j<childlist.getLength();j++){ Node sub_node = childlist.item(j); //Get the node type short type = sub_node.getNodeType(); //Determine whether the node type is different is #text, because the characters between the end of the previous tag > and the beginning of the next tag < will be marked as #text // So judgment needs to be made here if(type == Node.ELEMENT_NODE){ //Get all The node name and node value of the node System.out.println(sub_node.getNodeName() + " : " + sub_node.getTextContent()); } } } } }
The output is as follows
2
id : 1001
name: JAVA Advanced Programming
author: Zhang San
price : 85.72
id : 1002
name: C++ and C#
author: Li Mishui
price : 125.73
4 SAX method to parse XML files
4.1 Create a DeaultHandler subclass to parse XML documents
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class BookDefaultHandler extends DefaultHandler { /** * Called when parsing xml documents */ public void startDocument( ) throws SAXException { System.out.println("Start parsing XML document"); super.startDocument(); } /** * Called when parsing the xml document ends */ public void endDocument() throws SAXException { super.endDocument() ; System.out.println("Complete parsing XML document"); } /** * Used when starting to parse XML document node */ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement (uri, localName, qName, attributes); //Determine if it is a book node and get the node attributes and attribute values if(qName. equals("book")){ //Get the number of current attributes int len = attributes.getLength(); //Loop to get each attribute for(int i=0;i<len;i++){ //Attribute name String name = attributes.getQName(i); //Attribute value String value = attributes.getValue(i); System.out.println("Attribute name: " + name + "\tAttribute value: " + value); } }else if(!"books".equals(qName) && !"book".equals(qName)){ System.out.print("Name of node: " + qName + "\t"); } } /** * Used at the end of parsing XML document nodes */ public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); } /** * Parse node text content */ public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); //Convert node text to string String value = new String(ch, start, length); //Exclude empty data and output node text if(!"".equals(value.trim())){ System.out.println(value); } } }
4.2 Steps to parse XML data using SAX
a. Create a SAXParserFactory object
b. Create a SAXParser object (as a parser)
c. Create a DefaultHandler subclass object
d. Call the parse method
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class TestSax4Xml { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //a. Create a SAXParserFactory object SAXParserFactory spf = SAXParserFactory.newInstance(); //b. Create a SAXParser object (as a parser) SAXParser sp = spf.newSAXParser() ; //c. Create a DefaultHandler subclass object BookDefaultHandler bdh = new BookDefaultHandler(); //d. Call the parse method of the SAXParser object sp.parse("book.xml", bdh); } }
4.3 Output results
Start parsing XML document
Attribute name: id Attribute value: 1001
The name of the node: name JAVA Advanced Programming
Node name: author Zhang San
Node name: price 85.72
Attribute name: id Attribute value: 1002
The name of the node: name C++ and C#
Node name: author Li Mishui
Node name: price 125.73
Complete parsing XML document
5 JDOM method to parse XML data
5.1 Steps
a. Create a SAXBuilder object
b. Call the build method and obtain the Document object through the IO stream
c. Get the root node
d. Get the collection of direct child nodes under the root node
e. Traverse the collection
import java.io.FileInputStream; import java.io.IOException; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; public class TestJdom4Xml { public static void main(String[] args) throws JDOMException, IOException { //a. Create a SAXBuilder object SAXBuilder sb = new SAXBuilder(); //b. Call the build method, Obtain the Document object through the OI stream Document doc = sb.build(new FileInputStream("src/book.xml")); //c. Get the root node Element root = doc.getRootElement(); //d. Get the root node A collection of direct child nodes List<Element> books = root.getChildren(); //e. Traverse the collection and get each child node for(int i=0;i<books.size();i++){ //Get the element Element in the collection book = books.get(i); //Get the attribute collection List<Attribute> under the current node atts = book.getAttributes(); for(int j=0;j<atts.size();j++){ // Output attribute name: attribute value System.out.println(atts.get(j).getName() + "\t" + atts.get(j).getValue()); } // Get the list of child nodes under the current node < Element> subEles = book.getChildren(); //Traverse the child nodes and get the name and text value for(Element e : subEles){ System.out.println(e.getName() + "\t" + e.getValue()); } } } }
5.2 Output results
id 1001
name JAVA Advanced Programming
author Zhang San
price 85.72
id 1002
name C++ and C#
author Li Mishui
price 125.73
6 DOM4J parsing XML
6.1 DOM4J XML parsing steps
a. Create a SAXReader object
b. Call the read method
c. Get the root node
d. Traverse direct nodes through an iterator
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class TestDom4jXml { public static void main(String[] args) throws FileNotFoundException, DocumentException { //a. Create a SAXReader object SAXReader sr = new SAXReader(); //b. Call the read method Document doc = sr.read(new FileInputStream("src/book.xml")); //c. Get the root node Element root = doc.getRootElement(); //d. Traverse the direct node through the iterator for(Iterator<Element> iter=root.elementIterator();iter.hasNext();){ Element book = iter.next(); //Get all attributes under the nodeIterator <Attribute> arrts = book.attributeIterator(); //Traverse attribute information while(arrts.hasNext()){ Attribute at = arrts.next(); String name = at.getName(); String value = at.getValue(); System.out.println("Node attributes :" + name + "\t" + value); } //Traverse the sub-nodes under the node Iterator<Element> subele = book.elementIterator(); //Get all node names and text values under the sub-node while(subele.hasNext( )){ Element node = subele.next(); System.out.println(node.getName() + "\t" + node.getText()); } } } }
6.2 Output results
Node attributes: id 1001
name JAVA Advanced Programming
author Zhang San
price 85.72
Node attributes: id 1002
name C++ and C#
author Li Mishui
price 125.73