XML parsing basics

Source: Internet
Author: User
Java Web development is a typical practice. in project development, the main function of HTML is to display data. XML is required to standardize the data storage structure. XML has its own syntax, and all the tag elements can be defined by the user. 1. understand XMLXML (eXtendedMarkupLanguage, scalable markup language) and provide a set of data description methods for cross-platform, cross-network, and cross-program languages, common functions such as data exchange, system configuration, and content management can be easily implemented using XML...

Java Web development practices

In project development, the main function of HTML is to display data. to standardize the data storage structure, XML is required. XML has its own syntax, and all the tag elements can be defined by the user.

1. understand XML

XML (eXtended Markup Language) provides a set of data description methods for cross-platform, cross-network, and cross-program languages, XML allows you to conveniently implement common functions such as data exchange, system configuration, and content management.
Similar to HTML, XML is a markup language. At the same time, the elements in HTML are fixed and display-oriented. tags in XML are user-defined and mainly stored as data.

(5) read XML information.

// Xml_demo.xml
     
         
  
   
James
          
  
   
Asaasa@163.com
      
     
         
  
   
Xiao Zhang
          
  
   
Xiaoli@163.com
      
 

DOM completes XML reading.

Package com. demo; 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. element; import org. w3c. dom. nodeList; import org. xml. sax. SAXException; public class XmlDomDemo {public static void main (String [] args) {// (1) create DocumentBuilderFactory to obtain DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // (2) obtain DocumentBuilder builder = null through DocumentBuilderFactory; try {builder = factory. newDocumentBuilder ();} catch (ParserConfigurationException e) {e. printStackTrace ();} // (3) defines the Document interface object. Using the DocumentBuilder class to perform the DOM tree operation is the conversion operation Document doc = null; try {// read the XML file in the specified path, read to memory doc = builder. parse ("xml_demo.xml");} catch (SAXException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} // (4) search for the linkman node NodeList nl = doc. getElementsByTagName ("linkman"); // (5) output the content of the text node in the first subnode of NodeList for (int I = 0; I <nl. getLength (); I ++) {// retrieve each Element element Element = (Element) nl. item (I); System. out. println ("name:" + element. getElementsByTagName ("name "). item (0 ). getFirstChild (). getNodeValue (); System. out. println ("email:" + element. getElementsByTagName ("email "). item (0 ). getFirstChild (). getNodeValue ());}}}

DOM completes XML file output.
In this case, you need to use each interface (such as the Element interface) provided in the DOM operation and manually set the relationship between each node. at the same time, when creating a Document object, you must use newDocument () method to create a new DOM tree.
If you want to save the XML file on the hard disk, you need to use TransformerFactory, Transformer, DOMSource, and StreamResult.
TransformerFactory class: obtains the instance object of a Transformer class.
DOMSource class: Receives Document objects.
StreamResult class: specifies the output stream object to be used (it can be output to a file or a specified output stream ).
Transformer class: This class is used to output content.

StreamResult class constructor

Package com. demo; import java. io. file; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import javax. xml. transform. outputKeys; import javax. xml. transform. transformer; import javax. xml. transform. transformerConfigurationException; import javax. xml. transform. transformerException; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import org. w3c. dom. element; public class XmlDemoWrite {public static void main (String [] args) {// (1) create DocumentBuilderFactory to obtain DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // (2) obtain DocumentBuilder builder = null through DocumentBuilderFactory; try {builder = factory. newDocumentBuilder ();} catch (ParserConfigurationException e) {e. printStackTrace ();} // (3) defines the Document interface object. Using the DocumentBuilder class to perform the DOM tree conversion operation, Document doc = null; // creates a new Document doc = builder. newDocument (); // (4) Create each operation node Element addresslist = doc. createElement ("addresslist"); Element linkman = doc. createElement ("linkman"); Element name = doc. createElement ("name"); Element email = doc. createElement ("email"); // (5) set the text content of the node, that is, add the text node name for each node. appendChild (doc. createTextNode ("James"); email. appendChild (doc. createTextNode ("xiaoming@163.com"); // (6) set the node link linkman. appendChild (name); linkman. appendChild (email); addresslist. appendChild (linkman); doc. appendChild (addresslist); // (7) output the document to the file TransformerFactory tf = TransformerFactory. newInstance (); Transformer t = null; try {t = tf. newTransformer ();} catch (TransformerConfigurationException e) {e. printStackTrace ();} // sets the encoding t. setOutputProperty (OutputKeys. ENCODING, "GBK"); // output document DOMSource source = new DOMSource (doc); // specify the output position StreamResult result = new StreamResult (new File ("xml_wirte.xml ")); try {// Output t. transform (source, result); System. out. println ("yes");} catch (TransformerException e) {e. printStackTrace ();}}}

Generate document:

// Xml_write.xml
     
         
  
   
James
          
  
   
Xiaoming@163.com
      
 

2.2. SAX parsing operations

Unlike DOM operations, SAX uses a sequential access mode, is a quick way to read XML data.
A series of events are triggered when you use the SAX parser for operations.

If you want to use SAX parsing in development, you should first compile a SAX parser, then define a class directly, and make the class inherit from the DefaultHandler class, at the same time, you can override the methods in the above table.

Package com. sax. demo; import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; public class XmlSax extends DefaultHandler {@ Override public void startDocument () throws SAXException {System. out. println ("
 ") ;}@ Override public void endDocument () throws SAXException {System. out. println (" \ n file reading ends... ") ;}@ Override public void startElement (String url, String localName, String name, Attributes attributes) throws SAXException {System. out. print ("<"); System. out. print (name); if (attributes! = Null) {for (int x = 0; x <attributes. getLength (); x ++) {System. out. print ("" + attributes. getQName (x) + "= \" "+ attributes. getValue (x) + "\" ") ;}} System. out. print (">") ;}@ Override public void characters (char [] ch, int start, int length) throws SAXException {System. out. print (new String (ch, start, length) ;}@ Override public void endElement (String url, String localName, String name) throws SAXException {System. out. print ("
 ");}}

After the SAXParserFactory and SAXParser objects are created, the XML file to be parsed and the specified SAX parser are specified using the SAXPaeser parse () method.

Create the file to be read: sax_demo.xml

     
         
  
   
James
          
  
   
Xiaoming@163.com
      
     
         
  
   
Xiao Li
          
  
   
Xiaoli@163.com
      
 

Use the SAX parser

Package com. sax. demo; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; public class SaxTest {public static void main (String [] args) throws Exception {// (1) create a SAX parsing factory SAXParserFactory factory = SAXParserFactory. newInstance (); // (2) constructor SAXParser parser = factory. newSAXParser (); // (3) parse XML using handler parser. parse ("sax_demo.xml", new XmlSax ());}}

Through the above program, we can find that using SAX resolution is easier than using DOM resolution.

Main operations of jdom

Use jdom to generate XML files

Package com. jdom. demo; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import org. jdom. attribute; import org. jdom. document; import org. jdom. element; import org. jdom. output. XMLOutputter; public class WriteXml {public static void main (String [] args) {// defines the node Element addresslist = new Element ("addresslist "); element linkman = new Element ("linkman"); Element name = new Element ("name"); Element email = new Element ("email "); // define Attribute id = new Attribute ("id", "xm"); // declare a Document object Document doc = new Document (addresslist ); // Set the element content name. setText ("James"); email. setText ("xiaoming@163.com"); name. setAttribute (id); // Set the subnode linkman. addContent (name); linkman. addContent (email); // add linkman to the root node addresslist. addContent (linkman); // Used to output the XML file XMLOutputter out = new XMLOutputter (); // Set the output encoding out. setFormat (out. getFormat (). setEncoding ("GBK"); // output the XML file try {out. output (doc, new FileOutputStream ("jdom_write.xml");} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
// Jdom_write.xml
     
         
  
   
James
          
  
   
Xiaoming@163.com
      
 

Use jdom to read XML files

Package com. jdom. demo; import java. io. IOException; import java. util. list; import org. jdom. document; import org. jdom. element; import org. jdom. JDOMException; import org. jdom. input. SAXBuilder; public class ReadXml {public static void main (String [] args) throws JDOMException, IOException {// Create a SAX parsing SAXBuilder builder = new SAXBuilder (); // find Document readDoc = builder. build ("jdom_write.xml"); // read the root Element stu = readDoc. getRootElement (); // obtain the List of all linkman sub-elements = stu. getChildren ("linkman"); for (int I = 0; I <list. size (); I ++) {Element e = (Element) list. get (I); String name = e. getChildText ("name"); String id = e. getChild ("name "). getAttribute ("id "). getValue (); String email = e. getChildText ("email"); System. out. println ("---- Contact ----"); System. out. println ("name:" + name + "No.:" + id); System. out. println ("Email:" + email );}}}

Jdom is a common operation component.
It is widely used in actual development.

2.4. parsing tool: dom4j

Dom4j is also a set of XML operation component packages mainly used to read and write XML files. because dom4j has excellent performance, powerful functions, and ease of use, it has been widely used. For example, dom4j is used in Hibernate and Spring frameworks for XML parsing.
Jar packages to be introduced during development: dom4j-1.6.1.jar, lib/jaxen-1.1-beta-6.jar

The operation interfaces used in dom4j are defined in the org. dom4j package. Use other packages as needed.

Main dom4j interfaces

Use dom4j to generate an XML file:

Package com. dom4j. demo; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. unsupportedEncodingException; import org. dom4j. document; import org. dom4j. export enthelper; import org. dom4j. element; import org. dom4j. io. outputFormat; import org. dom4j. io. XMLWriter; public class Dom4jWrite {public static void main (String [] args) {// create Document doc = policenthelper. createDocument (); // defines the node Element addresslist = doc. addElement ("addresslist"); Element linkman = addresslist. addElement ("linkman"); Element name = linkman. addElement ("name"); Element email = linkman. addElement ("email"); // Set the node content name. setText ("James"); email. setText ("xiaoming@163.com"); // set output format OutputFormat format = OutputFormat. createPrettyPrint (); // specify the output encoding format. setEncoding ("GBK"); try {XMLWriter writer = new XMLWriter (new FileOutputStream (new File ("dom4j_demo.xml"), format); writer. write (doc); writer. close ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
// Dom4j_demo.xml
   
     
  
   
James
      
  
   
Xiaoming@163.com
    
 

Use dom4j to read XML files:

Package com. dom4j. demo; import java. io. file; import java. util. iterator; import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. element; import org. dom4j. io. SAXReader; public class Dom4jRead {public static void main (String [] args) {// read File file = new File ("dom4j_demo.xml "); // Create a SAX parsing to read SAXReader reader = new SAXReader (); Document doc = null; try {// read Document doc = reader. read (file);} catch (incluentexception e) {e. printStackTrace ();} // Obtain the root Element root = doc. getRootElement (); // get all the sub-nodes Iterator iter = root. elementIterator (); while (iter. hasNext () {// Obtain each linkman Element linkman = (Element) iter. next (); System. out. println ("name:" + linkman. elementText ("name"); System. out. println ("email address:" + linkman. elementText ("email "));}}}
Summary
  • XML is mainly used for data exchange, while HTML is used for display.

  • Java provides two types of XML parsing methods: DOM and SAX. The differences between the two types of resolution are as follows:

    • DOM parsing reads all the content to the memory and forms a memory tree. if the file volume is large, it cannot be used, but DOM parsing can modify the file.

    • SAX parsing reads XML files in sequence. it is not restricted by the file size, but cannot be modified.

  • XML parsing can use jdom and dom4j third-party toolkit to improve development efficiency.

The above is the basic content of XML parsing. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.