Dom generation and parsing of XML documents

Source: Internet
Author: User

Dom generation and parsing of XML documents

Defines a set of interfaces for the parsed versions of the XML document. The parser reads the entire document, constructs a memory-resident tree structure, and then the code can use the DOM interface to operate on this tree structure. Advantage: the entire document tree is in the memory for easy operation. It supports multiple features such as deletion, modification, and rescheduling. disadvantage: transferring the entire document to the memory (including useless nodes ), A waste of time and space. Use Cases: Once the documents are parsed, the data needs to be accessed multiple times. There are sufficient hardware resources (memory and CPU ). (XML is parsed in the same way in different languages, but the syntax is different. There are two basic parsing Methods: Sax and Dom. Sax is based on event stream parsing, and Dom is based on XML document tree structure parsing. There is also the use of plug-ins (dom4j, JDOM and Other plug-ins) to parse)

Package E; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. printwriter; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; import javax. XML. transform. outputkeys; import javax. XML. t Ransform. 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; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; import Org. XML. sax. Saxexception;/***** @ author hongliang. dinghl * Dom generation and resolution XML document */public class domdemo {private document; private string filename; Public void Init () {try {documentbuilderfactory factory = documentbuilderfactory. newinstance (); documentbuilder builder = factory. newdocumentbuilder (); this.doc ument = builder. newdocument ();} catch (parserconfigurationexception e) {system. out. println (E. g Etmessage () ;}} public void createxml (string filename) {element root = this.doc ument. createelement ("employees"); this.doc ument. appendchild (Root); element Employee = this.doc ument. createelement ("employee"); element name = this.doc ument. createelement ("name"); name.appendchild(this.doc ument. createtextnode ("ding hongliang"); employee. appendchild (name); element sex = this.doc ument. createelement ("sex"); sex. AP Pendchild(this.doc ument. createtextnode ("M"); employee. appendchild (sex); element age = this.doc ument. createelement ("Age"); age.appendchild(this.doc ument. createtextnode ("30"); employee. appendchild (AGE); root. appendchild (employee); transformerfactory TF = transformerfactory. newinstance (); try {transformer = TF. newtransformer (); domsource source = new domsource (document); transformer. se Toutputproperty (outputkeys. encoding, "gb2312"); transformer. setoutputproperty (outputkeys. indent, "yes"); printwriter PW = new printwriter (New fileoutputstream (filename); streamresult result = new streamresult (PW); transformer. transform (source, result); system. out. println ("XML file generated successfully! ");} Catch (transformerconfigurationexception e) {system. out. println (E. getmessage ();} catch (illegalargumentexception e) {system. out. println (E. getmessage ();} catch (filenotfoundexception e) {system. out. println (E. getmessage ();} catch (transformerexception e) {system. out. println (E. getmessage () ;}} public void parserxml (string filename) {try {documentbuilderfactory DBF = documentbuilder Factory. newinstance (); documentbuilder DB = DBF. newdocumentbuilder (); document = dB. parse ("C: \ 111.txt");/* <? XML version = "1.0" encoding = "UTF-8"?> <Employees> <employee> <Name> ddviplinux </Name> <sex> m </sex> <age> 30 </age> </employee> <Name> ddvi222 </Name> <sex> m </sex> <age> 56 </age> </employee> </employees> */nodelist employees = document. getchildnodes (); For (INT I = 0; I <employees. getlength (); I ++) {node Employee = employees. item (I); // employeesnodelist employeeinfo = employee. getchildnodes (); For (Int J = 0; j <employeeinfo. getlength (); j ++) {node = employeeinfo. item (j); nodelist employeemeta = node. getchildnodes (); // employeefor (int K = 0; k <employeemeta. getlength (); k ++) {If ("name ". equals (employeemeta. item (k ). getnodename () | "sex ". equals (employeemeta. item (k ). getnodename () | "age ". equals (employeemeta. item (k ). getnodename () {system. out. println (employeemeta. item (k ). getnodename () + ":" + employeemeta. item (k ). gettextcontent () ;}}} system. out. println ("parsed");} catch (filenotfoundexception e) {system. out. println (E. getmessage ();} catch (parserconfigurationexception e) {system. out. println (E. getmessage ();} catch (saxexception e) {system. out. println (E. getmessage ();} catch (ioexception e) {system. out. println (E. getmessage ());}}}

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.