Java basics --- XML parsing (1), basics --- xml
XML is a scalable Markup Language
In the XML file, because more information is described, you should use the definition name of the element installed in the program to remove the corresponding content after obtaining an xml file, such an operation is called XML parsing.
In XML parsing, W3C defines the parsing methods of the SAX and DOM. program operations of these two parsing methods are as follows:
It can be seen that the application does not directly perform operations on the XML document, but first analyzes the Import Analysis of the XML document by XML, then, the program uses the DOM interface or the SAX interface provided by the XML analyzer to perform input operations on the analysis result, thereby indirectly implementing access to the XML document. Perform the following operations
DOM Parsing
XML parsing in the DOM mode refers to processing the entire XML document as an object. The entire document is first read into the memory. Is a tree-based structure. Generally, You need to load the entire document and construct the DOM tree before you can start to work.
Dom_demo.xml File
<?xml version="1.0" encoding="GBK"?><addresslist> <linkman> <name>żmyҮ</name> <email>oumyye@163.com</email> </linkman> <linkman> <name>oumyye</name> <email>oumyye@qq.com</email> </linkman></addresslist>
DOMDemo. java parsing File
Package com. xml. dom parsing; import java. io. *; import org. w3c. dom. *; import javax. xml. parsers. *; public class DOMDemo {public static void main (String args []) throws Exception {// get the DocumentBuilderFactory class Object DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // obtain the DocumentBuilder build = factory object of the DocumentBuilder class. newDocumentBuilder (); Document doc = build. parse (new File ("E:" + File. separator + "dom_demo.xml"); NodeList nl = doc. getElementsByTagName ("name"); // output node System. out. println ("name:" + nl. item (0 ). getFirstChild (). getNodeValue ());}}
Running result:
SAX Parsing
Unlike DOM operations, SAX uses an ordered mode for access and a quick way to read XML data, A series of events are triggered when you use the SAX Parser for operations, as shown in. When the scanning ends and ends, the relevant processing methods are called at the beginning and end of the element, and make corresponding operations.
Sax_demo.xm File
<? Xml version = "1.0" encoding = "GBK"?> <Addresslist> <linkman id = "omy" name = "Pants"> <name> mE </name> <email> oumyye@163.com </email> </linkman> <linkman id = "ol" name = ""> <name> even </name> <email> oumyye@qq.com </email> </linkman> </addresslist>
MySAX. java source code:
Package com. xml. sax parsing; import java. io. file; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. *; import org. xml. sax. helpers. *; public class MySAX extends DefaultHandler {public void startDocument () throws SAXException {System. out. println ("<? Xml version = \ "1.0 \" encoding = \ "GBK \"> ");} public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {System. out. print ("<"); System. out. print (qName); if (attributes! = Null) {// if the property for (int x = 0; x <attributes. getLength (); x ++) {System. out. print ("" + attributes. getQName (x) + "= \" "+ attributes. getValue (x) + "\" ") ;}} System. out. print (">");} public void endElement (String uri, String localName, String qName) throws SAXException {System. out. print ("<"); System. out. print (qName); System. out. print (">");} public void characters (char [] ch, int start, int l Ength) throws SAXException {System. out. print (new String (ch, start, length);} public void endDocument () throws SAXException {System. out. println ("document ended... ");} Public static void main (String args []) throws Exception {// create a SAX parsing factory SAXParserFactory factory = SAXParserFactory. newInstance (); SAXParser parser = factory. newSAXParser (); parser. parse ("e:" + File. separator + "sax_demo.xml", new MySAX ());}}
Resolution result:
Let's take a look at the differences between DOM and SAX: