XML parsing of Java Foundation---->java

Source: Internet
Author: User

When looking at the source code of the company framework, it is found that the framework uses the XML parsing that comes with the JDK. Today, think about summarizing it.

Create a document using the XML that comes with the JDK. The Createxmlfile class is as follows:
 PackageCom.huhx.saxxml;ImportJava.io.FileOutputStream;ImportJava.io.PrintWriter;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;ImportJavax.xml.transform.OutputKeys;ImportJavax.xml.transform.Transformer;Importjavax.xml.transform.TransformerFactory;ImportJavax.xml.transform.dom.DOMSource;ImportJavax.xml.transform.stream.StreamResult;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element; Public classCreatexmlfile { Public Static voidparsexml (String path) {documentbuilderfactory builderfactory=documentbuilderfactory.newinstance (); Try{documentbuilder Dombuilder=Builderfactory.newdocumentbuilder (); Document Document=dombuilder.newdocument (); Element Root= Document.createelement ("Persons");            Document.appendchild (root); //First PersonElement Person1 = document.createelement ("Person"); Person1.setattribute ("Name", "Linux");            Root.appendchild (Person1); Element Sex= document.createelement ("Sex"); Sex.appendchild (document.createTextNode (Female));            Person1.appendchild (Sex); Element Address= Document.createelement ("Address"); Address.appendchild (document.createTextNode (Hubei));            Person1.appendchild (address); Element PhoneNumber= Document.createelement ("Phone"); Phonenumber.appendchild (document.createTextNode ("155124578**"));            Person1.appendchild (PhoneNumber); //a second personElement Person2 = document.createelement ("Person"); Person2.setattribute ("Name", "Huhx");            Root.appendchild (Person2); Element Sex2= document.createelement ("Sex"); Sex2.appendchild (document.createTextNode (Male));            Person2.appendchild (SEX2); Element Address2= Document.createelement ("Address"); Address2.appendchild (document.createTextNode (Hunan));            Person2.appendchild (ADDRESS2); Element PhoneNumber2= Document.createelement ("Phone"); Phonenumber2.appendchild (document.createTextNode ("155124578**"));            Person2.appendchild (PHONENUMBER2); //write the above document to a fileTransformerfactory tf =transformerfactory.newinstance (); Transformer Transformer=Tf.newtransformer (); Domsource Source=NewDomsource (document); Transformer.setoutputproperty (outputkeys.encoding,"UTF-8"); Transformer.setoutputproperty (Outputkeys.standalone,"Yes"); Transformer.setoutputproperty (Outputkeys.indent,"Yes"); PrintWriter PW=NewPrintWriter (NewFileOutputStream (path)); Streamresult result=NewStreamresult (PW);        Transformer.transform (source, result); } Catch(Exception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {Parsexml ("Text.xml"); }}

Second, the generated Text.xml file is as follows:
<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><Persons>    < Personname= "Linux">        <Sex>Woman</Sex>        <Address>Hubei</Address>        <Phone>155124578**</Phone>    </ Person>    < Personname= "Huhx">        <Sex>Man</Sex>        <Address>Hunan</Address>        <Phone>155124578**</Phone>    </ Person></Persons>

Parsing documents using the XML that comes with the JDK the contents of the Readxmlfile.java class are as follows:
 PackageCom.huhx.saxxml;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;Importorg.w3c.dom.NodeList; Public classReadxmlfile { Public Static voidreadXml (String path) {Try{documentbuilderfactory builderfactory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Builderfactory.newdocumentbuilder (); Document Document=builder.parse (path); Element rootelement=document.getdocumentelement (); System.out.println ("root element:" +rootelement.getnodename ()); NodeList ChildNodes= Rootelement.getelementsbytagname ("Person");  for(inti = 0; I < childnodes.getlength (); i++) {element element=(Element) Childnodes.item (i); String name= Element.getattribute ("name"); String content= Element.getelementsbytagname ("Sex"). Item (0). Getfirstchild (). Getnodevalue (); String Address= Element.getelementsbytagname ("Address"). Item (0). Getfirstchild (). Getnodevalue (); String Phone= Element.getelementsbytagname ("Phone"). Item (0). Getfirstchild (). Getnodevalue (); System.out.println ("Name:" + name + ", Content:" + content + ", Address:" + address + ", Phone:" +phone); }        } Catch(Exception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {readXml ("Text.xml"); }}

Second, the console printing results are as follows:
155124578**155124578**

Friendship Link

XML parsing of Java Foundation---->java

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.