Rookie how to learn XML (Java)

Source: Internet
Author: User
Tags xml parser

1. Reading XML

2. Writing XML

3. Create a new XML

4. Creating the Construction XML

To create a new XML document:

1/constructs an XML parser and creates a new document/21 Document document =NULL;32 Documentbuilderfactory factory =documentbuilderfactory.newinstance ();43Try {54 Documentbuilder builder =Factory.newdocumentbuilder ();65 document = Builder.newdocument ();//to create a new XML file76//document = Builder.parse (fileName);//parsing an existing XML file87}Catch(parserconfigurationexception e) {98e.printstacktrace ();Ten9}

Output the in-memory XML to the file:

1 //output the in-memory XML to the file2Transformer tf =NULL;3Transformerfactory TFF =transformerfactory.newinstance ();4         Try {5tf =Tff.newtransformer ();6Tf.transform (NewDomsource (document),NewStreamresult ("Src/test.xml"));7 8}Catch(transformerconfigurationexception e) {9 e.printstacktrace ();Ten}Catch(transformerexception e) { One e.printstacktrace (); A}

Building xml:

1Element root = document.createelement ("root");//creating the root directory2         3Element student = document.createelement ("Student");4         5Student.setattribute ("name", "Xiaoming");//set the property value of student6         7         /*to create a student child element*/8Element id = document.createelement ("id");9Element sex = document.createelement ("Sex");TenElement age = document.createelement (' Age '); One          A         /*set the text of an element*/ -Id.settextcontent ("0001"); -Sex.settextcontent ("Man"); theAge.settextcontent ("18"); -          -         /*to add a child element to a parent element (student)*/ - student.appendchild (ID); + student.appendchild (sex); - Student.appendchild (age); +          ARoot.appendchild (student);//Add an Element (node) student to the root directory (root) atDocument.appendchild (root);//Add the root directory to the document

Output Result:

1 <?XML version= "1.0" encoding= "UTF-8" standalone= "no"?>2 <Root>3     <Studentname= "Xiaoming">4         <ID>0001</ID>5         <Sex>Mans</Sex>6         < Age>18</ Age>7     </Student>8 </Root>

Get the data in the XML

1 <?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><Root>2     <Studentname= "Xiaoming">3         <ID>0001</ID>4         <Sex>Mans</Sex>5         < Age>18</ Age>6     </Student>7     <Studentname= "Xiaozhu">8         <ID>0002</ID>9         <Sex>Mans</Sex>Ten         < Age>20</ Age> One     </Student> A     <Studentname= "Xiaohong"> -         <ID>0003</ID> -         <Sex>Female</Sex> the         < Age>15</ Age> -     </Student> - </Root>

Finding operations on XML

1 //get all the information for property name=xiaoming in student2NodeList List_st =document.getelementsbytagname ("Student");3         /*iterate through all the student elements*/4          for(intI=0;i<list_st.getlength (); i++)5         {6Node n =List_st.item (i);7             8Element student= (element) n;//must be strong to element to get properties set, node cannot get Properties9             Ten             /*Find the node of attribute xiaoming in student*/ One             if(Student.getattribute ("name"). Equals ("Xiaoming")) A             { -                 /*get text information from all tags*/ -String id = student.getelementsbytagname ("id"). Item (0). Gettextcontent (); theString sex = student.getelementsbytagname ("Sex"). Item (0). Gettextcontent (); -String age = Student.getelementsbytagname ("Age"). Item (0). Gettextcontent (); -                  -SYSTEM.OUT.PRINTLN ("ID:" +ID); +System.out.println ("Sex:" +sex); -System.out.println ("Age:" +Age ); +                 /*Output: A * id:0001 at * Sex:man - * age:18 -                  */ -             } -}

Rookie how to learn XML (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.