Dom manipulating XML instances

Source: Internet
Author: User

The DTD is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><!List of ELEMENT books (Computer books) *><!ELEMENT Computer Books (title, price) +, author +, Introduction, TEL,HR)><!ELEMENT title (#PCDATA)><!ELEMENT Price (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT Introduction (#PCDATA)><!ELEMENT Tel (#PCDATA)><!ELEMENT HR EMPTY><!attlist Author sex CDATA #IMPLIED age CDATA #IMPLIED Phone CDATA #REQUIRED Job CDATA #FIXED "Writer" hobby CDATA "Talking">

The XML is as follows:

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><List of Books>    <Computer Books>        <title>Thinking in Java</title>        <Price>60</Price>        <title>Thinking in XX</title>        <Price>80</Price>        <Author Hobby= "Talking"Job= "Writer"Phone= "1212">Gdfd</author>        <Introduction>Java Bible</Introduction>        <Tel>13661955777</Tel>        <HR/>    </Computer Books>    <Computer Books>        <title>Thinking in C + +</title>        <Price>100</Price>        <Author age= " the"Hobby= "Talking"Job= "Writer"Phone= "1213">Qq</author>        <Introduction>C + + Bible</Introduction>        <Tel>13661955732</Tel>        <HR/>    </Computer Books>    <Computer Books>        <title>I Love C + +</title>        <Price>99</Price>        <Author age= "$"Hobby= "Talking"Job= "Writer"Phone= "139677">Hzz</author>        </Computer Books>    </List of Books>

The procedure is as follows:

 PackageCom.xml;ImportJavax.xml.*;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;ImportJavax.xml.transform.Transformer;Importjavax.xml.transform.TransformerConfigurationException;Importjavax.xml.transform.TransformerFactory;ImportJavax.xml.transform.dom.DOMSource;ImportJavax.xml.transform.stream.StreamResult;Importorg.w3c.dom.*;ImportCom.sun.org.apache.xalan.internal.xsltc.cmdline.Transform; Public classDomxml {/**     * @paramargs *@throwsException*/     Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stubdocumentbuilderfactory dbf=documentbuilderfactory.newinstance (); Documentbuilder DB=Dbf.newdocumentbuilder (); Document Document= Db.parse ("Webroot/newfile.xml"); //System.out.print (document);read (document);    Delete (document); }        //Delete book titled I love C + +     Public Static voidDelete (Document doc)throwsexception{NodeList nl= Doc.getelementsbytagname ("title"); Node N=NULL; Node P=NULL;  for(intI=0;i<nl.getlength (); i++){            if((N=nl.item (i)). Gettextcontent (). Equals ("I love C + +") ) {(P=N.getparentnode ()). Getparentnode (). removechild (P);  Break; }} transformerfactory TFF=transformerfactory.newinstance (); //by getting a converterTransformer tf =Tff.newtransformer (); Tf.transform (NewDomsource (DOC),NewStreamresult ("Webroot/newfile.xml")); }         Public Static voidAdd (Document doc)throwsexception{Element Newbook= Doc.createelement ("Computer book"); Element Newbook_name= Doc.createelement ("title"); Element Newbook_price= Doc.createelement ("Price"); Element Newbook_auth= Doc.createelement ("Author"); Newbook_name.settextcontent ("I Love C + +"); Newbook_price.settextcontent ("99"); Newbook_auth.settextcontent ("Hzz"); Newbook_auth.setattribute ("Phone", "139677"); Newbook_auth.setattribute ("Age", "44");        Newbook.appendchild (Newbook_name);        Newbook.appendchild (Newbook_price);                Newbook.appendchild (Newbook_auth); //Add new books to the list of booksdoc.getdocumentelement (). appendchild (Newbook); //Get TransformerfactoryTransformerfactory TFF =transformerfactory.newinstance (); //by getting a converterTransformer tf =Tff.newtransformer (); Tf.transform (NewDomsource (DOC),NewStreamresult ("Webroot/newfile.xml")); }         Public Static voidread (document document) {NodeList nl= document.getElementsByTagName ("Computer book"); Element e= (Element) nl.item (0); Element E2= (Element) e.getelementsbytagname ("Author"). Item (0); System.out.print (E2.gettextcontent ()+e2.getattribute ("Phone")); }         Public Static voidList (node node) {if(Node.getnodetype () = =node.        Element_node) System.out.println (Node.getnodename ()); ElseSystem.out.println (node.gettextcontent ()+node.getattributes ()); NodeList NL=node.getchildnodes ();  for(intI=0;i<nl.getlength (); i++) {Node n=Nl.item (i);        List (n); }    }}

Dom manipulating XML instances

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.