Dom parsing using JAXP vs. xml

Source: Internet
Author: User

/*dom Analytic programming
• Traverse all nodes
• Find a node
• Delete Nodes
• Update Nodes
• Adding nodes
/*


PackageCn.itcast.jaxp;ImportJava.io.File;Importjava.io.IOException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;ImportJavax.xml.transform.Transformer;Importjavax.xml.transform.TransformerException;Importjavax.xml.transform.TransformerFactory;ImportJavax.xml.transform.dom.DOMSource;ImportJavax.xml.transform.stream.StreamResult;Importorg.junit.Test;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDemo1 {/**manipulating XML documents with JAXP *@paramargs *@throwsparserconfigurationexception *@throwsIOException *@throwssaxexception*/ Public Static voidMain (string[] args)throwsparserconfigurationexception, Saxexception, IOException {//1. Get the factoryDocumentbuilderfactory factory =documentbuilderfactory.newinstance (); //2. Generating the parserDocumentbuilder Builder =Factory.newdocumentbuilder (); //3. Parse the XML document and get the document that represents itDocument document = Builder.parse (NewFile ("Src/book1.xml")); //Traverselist (document); } //get the value of the price junction@Test Public voidRead ()throwsexception{//1. Get the factoryDocumentbuilderfactory factory =documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book.xml")); NodeList List= document.getElementsByTagName ("Price"); Node Price= List.item (0); String value=price.gettextcontent (); System.out.println (value); } //Modify the value of the node:< price >39.00 yuan </price > change to 109@Test Public voidUpdate ()throwsexception{documentbuilderfactory Factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book1.xml")); Node Price= document.getElementsByTagName ("Price"). Item (0); Price.settextcontent ("109"); //write an in-memory document to an XML fileTransformerfactory tf =transformerfactory.newinstance (); //Get ConverterTransformer ts =Tf.newtransformer (); Ts.transform (NewDomsource (document),NewStreamresult (NewFile ("Src/book1.xml"))); } //Add a child node to the specified node (the price node)@Test Public voidAdd ()throwsexception{documentbuilderfactory Factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book1.xml")); //Create a node that needs to be increasedNode Price = document.createelement ("Prices"); Price.settextcontent ("59 Yuan"); //get the father who needs to increase the nodeNode parent = document.getElementsByTagName ("book"). Item (0); //hook up nodes that need to be added to the parent node.Parent.appendchild (price); Transformerfactory TF=transformerfactory.newinstance (); Transformer TS=Tf.newtransformer (); Ts.transform (NewDomsource (document),NewStreamresult (NewFile ("Src/book1.xml"))); } //inserts a price node into the specified position@Test Public voidADD2 ()throwsexception{documentbuilderfactory Factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book1.xml")); Node Node= Document.createelement ("Price"); Node.settextcontent ("39 Yuan"); Node Parent= document.getElementsByTagName ("book"). Item (0); Parent.insertbefore (node, document.getelementsbytagname ("title"). Item (0)); Transformerfactory TF=transformerfactory.newinstance (); Transformer TS=Tf.newtransformer (); Ts.transform (NewDomsource (document),NewStreamresult (NewFile ("Src/book1.xml"))); } //Delete the price node of an XML document@Test Public voidDelete ()throwsexception{documentbuilderfactory Factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book1.xml")); Node Node= document.getElementsByTagName ("Price"). Item (2); Node.getparentnode (). removechild (node); Transformerfactory TF=transformerfactory.newinstance (); Transformer TS=Tf.newtransformer (); Ts.transform (NewDomsource (document),NewStreamresult (NewFile ("Src/book1.xml"))); } //manipulating XML document Properties@Test Public voidUpdateattribute ()throwsexception{documentbuilderfactory Factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document= Builder.parse (NewFile ("Src/book1.xml")); //when manipulating elements of an XML document, the element is generally treated as a node object, but if the programmer finds that node is not good, it should turn node strong into the appropriate typeNode node = document.getelementsbytagname ("book"). Item (0); Element Book=NULL; if(Node.getnodetype () ==node.element_node) {//before you make a node transition, it's a good idea to first determine the node type .Book =(Element) node; } book.setattribute ("Name", "YYYYYYY"); Book.setattribute ("Password", "123"); Book.removeattribute ("Password"); Transformerfactory TF=transformerfactory.newinstance (); Transformer TS=Tf.newtransformer (); Ts.transform (NewDomsource (document),NewStreamresult (NewFile ("Src/book1.xml"))); } //Traverse Public Static voidList (node node) {if(Node.getnodetype () = =Node.element_node) {System.out.println (Node.getnodename ()); } NodeList List=node.getchildnodes (); for(intI=0;i<list.getlength (); i++) {Node child=List.item (i); List (child); } } }

Dom parsing using JAXP vs. xml

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.