XML parsing Technology

Source: Internet
Author: User

This article summarizes Dom,sax parsing and uses Java as a tool to parse XML documents.

1 Dom

Summary: Dom parsing XML is often referred to as xmldom (similar to htmldom technology), encapsulating XML documents into trees, with the benefit of tags (attributes) and dom trees in XML

Corresponds to node one by one in the. Nodes are objects so that you can manipulate the XML using the properties or methods of the object.

Cons: Every technology has its drawbacks, Dom parsing is not about processing an XML file with a large amount of data, because DOM technology directly parses the XML into a tree, loads it into memory,

This will consume more resources.

Take the following myclass.xml as an example

<?xml version= "1.0" encoding= "Utf-8"? ><class><stu id= "sp001" ><name> xiaoming </name><age >12</age><math>100</math><english>90</english></stu><stu id= "sp002" >    <name> Xiao Li </name><age>12</age><math>98</math><english>90</ English></stu></class>

  

(1) Code implementation

Parse XML first to get Document object documents

Public  void GetDocument (URL) {      documentbuilderfactory   dbf=documentbuilderfactory.newinstance ();// Create Factory      Documentbuilder Bu=dbf.newdocumentbuilder ();//dom parser      

  

* Next to add a node:

Public AddNode (Document doc) {//Create node    Element stu=doc. CreateElement ("Stu");   Element Name=doc. createelement ("name");   Element Age=doc. CreateElement ("Age");//Add attribute and text    stu.setattribute ("id", "sp003");//id attribute    name.settextcontent ("Xiao Liu");    Age.settextcontent ("11");//Add the node Stu to the document    Stu.appendchild (name);   Stu.appendchild (age);   Doc.getDocumentElement.getChild (stu);//Added to the XML document;    Call the Refresh function (custom), also on three words: Factory, object, refresh;   }

Note: What do you do with the Refresh function mentioned above?

We add the STU node to the document, note that it is not directly to the XML, but also in memory, and only refreshing into the XML document will make the new node visible in the XML;

So to brush, specifically how to write, I really forgot! This is recorded as refresh ();

* As for the amendment, delete that is the same. Not introduced

(2) Actually DOM technology can be used for XML and HTML, what is the similarity?

I learned Htmldom first, in HTML we usually use attributes to modify, set the value;

document.getElementById (' Stu '): id= "XXX";//Add id attribute (say is set also line);

document.getElementById (' name '): innertext= "XXX";//Add text;

The use of properties in HTML, the method is not used much, the reason is simple: property operations do not consider "browser-compatible";

Compared to the methods used in XML (basically no attributes), parsing an XML document has no "incompatible" things!

So the HTML will be, the parsing of XML is almost.

2 Sax parsing

Review:

Sax technology can be used to deal with the larger XML situation, DOM technology is not able to do
Why can't dom? Can sax?
The reason is that DOM technology directly parses the XML into a tree and loads it into memory, which consumes more resources.
Therefore, it is not suitable for processing XML with more data. Unlike Sax technology, he does not have to load XML directly into memory, just the data that is needed
Put it in. Memory resources are not consumed in large quantities. Sax technology is suitable for processing traversal, reading. Not suitable (cannot) be used to modify data.

Principle:

The SAX technique mainly uses the Defaulthander event processing object;

For example, there are methods:

& public void Startelement (string uri, String localname, String qName, Attributes Attributes) {//code}

Every visit to <> is called once;

See parameter: QName is the label name of <>, for example: <stu id= "sp002" > Then QName is stu. A attributes array that holds the properties of the label. Like what

Attributes.getvalue ("id")); it is "sp002"; We can handle it in code and get what we need!

& public void characters (char[] ch, int start, int length) {}//view Defaulthander document

(1) Realize

* Associating an XML file with an event-handling object

Public saxfunction (URL) {               saxparserfactory factory=saxparserfactory.newinstance ();   SAXParser Sax=factory.newsaxparser (); Sax.parse (Url,new myhander ());  Associating an XML file with an event-handling object, this object is custom!}

* Methods for overriding event object Defaulthander

Class Myhander extends defaulthandler{   private  String name= "";  associated property; @Overridepublic void characters (char[] ch, int start, int length) throws saxexception {      String  con=new Str ING (ch,start,length);              if (!con.trim (). Equals ("") && (this.name.equals (name) | | This.name.equals ("Age")))      System.out.println (con);} @Overridepublic void Startelement (String uri, String localname, String qName, Attributes Attributes) throws Saxexception { if (qname== "Stu")      {      System.out.println (attributes.getvalue ("id"));      }  }    }

  

Note: We can see the above code can get the attribute "id", the element name,age text; All are sax techniques that cannot be added, deleted, modified data. This is his fault.

Other XML parsing techniques such as: jdom,dom4j. Have time to discuss!

XML parsing Technology

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.