Modify XML file for Java manipulation of XML

Source: Internet
Author: User

Modify XML file for Java manipulation of XML

First, JAVA DOM PARSER

DOM Interfaces
The DOM defines several Java interfaces. Here is the most common interfaces:
Node-the Base datatype of the DOM.
Element-the vast majority of the objects you ' ll deal with is Elements.
Attr represents a attribute of an element.
Text the actual content of an Element or Attr.
Document represents the entire XML document. A Document object is an often referred to as a DOM tree.
Common DOM Methods
When you're working with the DOM, there is several methods you'll use often:
Document.getdocumentelement ()-Returns The root element of the document.
Node.getfirstchild ()-Returns The first child of a given Node.
Node.getlastchild ()-Returns The last child of a given Node.
Node.getnextsibling ()-These methods return the next sibling of a given Node.
Node.getprevioussibling ()-These methods return the previous sibling of a given Node.
Node.getattribute (Attrname)-for a given Node, returns the attribute with the requested name.

Second, source code: Modifyxmlfile.java

1  PackageCn.com.zfc.lesson26.xml;2 3 ImportJava.io.File;4 5 ImportJavax.xml.parsers.DocumentBuilder;6 Importjavax.xml.parsers.DocumentBuilderFactory;7 ImportJavax.xml.transform.Transformer;8 Importjavax.xml.transform.TransformerFactory;9 ImportJavax.xml.transform.dom.DOMSource;Ten ImportJavax.xml.transform.stream.StreamResult; One  A Importorg.w3c.dom.Document; - Importorg.w3c.dom.Element; - ImportOrg.w3c.dom.NamedNodeMap; the ImportOrg.w3c.dom.Node; -  - /** - * Using Java DOM PARSER: Modifying an XML file +  *  -  * @authorZFC + * @date December 7, 2017 pm 8:31:55 A  */ at  Public classModifyxmlfile { -      Public Static voidMain (string[] args) { -         Try { -String FilePath = "i:\\code_workspace\\javase_workspace\\javase\\src\\cn\\com\\zfc\\lesson26\\xml\\ Modifyxmlfile.xml "; -             //1. Creating a File object, mapping XML files -File File =NewFile (filePath); in             //2. Create a Documentbuilderfactory object to create a Documentbuilder object -Documentbuilderfactory documentbuilderfactory =documentbuilderfactory.newinstance (); to             //3. Create a Documentbuilder object to convert the XML file to a Document object +Documentbuilder Documentbuilder =Documentbuilderfactory.newdocumentbuilder (); -             //4. Create Document object, parse XML file theDocument document =documentbuilder.parse (file); *             //Modify the first student $             //5. Get the first Student nodePanax NotoginsengNode student = document.getElementsByTagName ("Student"). Item (0); -             //get all the properties of a node student node theNamedNodeMap NamedNodeMap =student.getattributes (); +             //Gets the attribute ID that defines the student ANode id = namednodemap.getnameditem ("id"); the             //Reset value for attribute ID +Id.settextcontent ("student11"); -             //6, obtain the root node students the first direct sub-node student $Student = document.getElementsByTagName ("Student"). Item (0); $Element studentelement =(Element) student; -             //7. Get the direct sub-node of node student name, sex . -Node name = Studentelement.getelementsbytagname ("name"). Item (0); theNode sex = studentelement.getelementsbytagname ("Sex"). Item (0); -Element nameelement =(Element) name;WuyiElement sexelement =(Element) sex; the             //8. Set the value to the node -Nameelement.settextcontent ("TomTom"); WuSexelement.settextcontent ("Femalefemale"); -             //9. Create a Transformerfactory object AboutTransformerfactory transformerfactory =transformerfactory.newinstance (); $             //10. Create a Transformer object -Transformer Transformer =Transformerfactory.newtransformer (); -             //11. Create a Domsource object -Domsource Domsource =NewDomsource (document); A             //12. Create a Streamresult object +Streamresult Restreamresult =Newstreamresult (file); the Transformer.transform (Domsource, restreamresult); -  $             //Output Test Results theStreamresult Consoleresult =NewStreamresult (System.out); the Transformer.transform (Domsource, consoleresult); the  the}Catch(Exception e) { - e.printstacktrace (); in         } the     } the}

Modifyxmlfile.xml

1 <?XML version= "1.0" encoding= "UTF-8" standalone= "no"?>2 <Students>3     <StudentID= "Student1">4         <name>Tom</name>5         <Sex>Female</Sex>6     </Student>7     <StudentID= "Student2">8         <name>Lucy</name>9         <Sex>Male</Sex>Ten     </Student> One </Students>

Third, the operation effect:

1 <?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><Students>2     <StudentID= "Student11">3         <name>TomTom</name>4         <Sex>Femalefemale</Sex>5     </Student>6     <StudentID= "Student2">7         <name>Lucy</name>8         <Sex>Male</Sex>9     </Student>Ten </Students>

Modify XML file for Java manipulation of XML

Related Article

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.