Java generates and parses XML

Source: Internet
Author: User

The main use of this article is the Jdom.jar package (download Baidu package) to achieve the generation of XML files and parsing XML files

The following is an implementation of generating XML

Description: The Stulists collection is a collection that holds the student object

1 ImportJava.io.File;2 Importjava.io.FileNotFoundException;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 Importjava.util.ArrayList;6 7 Importorg.jdom.Document;8 Importorg.jdom.Element;9 ImportOrg.jdom.input.SAXBuilder;Ten ImportOrg.jdom.output.Format; One ImportOrg.jdom.output.XMLOutputter; A  -  Public classAddxml { -      Public Static voidMain (string[] args) { the         Newaddxml (). Changexml ();  -     } -      Public voidChangexml () { -JDBC JDBC =NewJdbc (); + jdbc.addlist (); -Arraylist<student> stulists =jdbc.getstulist (); +          ADocument docu =NewDocument (); atElement root =NewElement ("root"); - docu.addcontent (root); -          for(inti = 0; I < stulists.size (); i++) { - //System.out.println (Stulists.get (i)); -Student s =Stulists.get (i); -              inElement info =NewElement ("Info"); -Element student =NewElement ("Student"); toElement ID =NewElement ("id"); +Element name =NewElement ("name"); -Element sex =NewElement ("Sex"); theElement age =NewElement ("Age"); *              $Element book =NewElement ("book");Panax NotoginsengElement bid =NewElement ("Bid"); -Element bname =NewElement ("Bname"); theElement Bprice =NewElement ("Bprice"); +Element Bautor =NewElement ("Bautor"); A book.addcontent (BID); the book.addcontent (bname); + book.addcontent (bprice); - book.addcontent (bautor); $              $ student.addcontent (ID); - student.addcontent (name); - student.addcontent (sex); the student.addcontent (age); - info.addcontent (student);Wuyi info.addcontent (book); the              - root.addcontent (info); Wu             intA = I+1; -String No = "000" +A; AboutStudent.setattribute ("No", No); $ Id.settext (S.getid ()); - Name.settext (S.getname ()); - Sex.settext (S.getsex ()); - Age.settext (S.getage ()); A              +String b= "0" +A; the Bid.settext (b); -Bname.settext ("Java core"); $Bprice.settext ("1334.0"); theBautor.settext ("Star"); the                  the         } the             //format the generated XML file, if not formatted, the resulting XML file will be a long line ... -Format format =Format.getcompactformat ();  inFormat.setencoding ("Utf-8");  theFormat.setindent ("");  theXmloutputter XO =Newxmloutputter (format); About         Try { theXo.output (Docu,NewFileOutputStream (NewFile ("E:/io/stu.xml"))); the}Catch(FileNotFoundException e) { the             //TODO auto-generated Catch block + e.printstacktrace (); -}Catch(IOException e) { the             //TODO auto-generated Catch blockBayi e.printstacktrace (); the         } theSYSTEM.OUT.PRINTLN ("Generate XML file successfully!!!" "); -     } -}

Results:

The following is a Java parsing of the XML file written above

Abbreviated version

1  PackageCom.direct.demo;2 3 Importjava.io.IOException;4 Importjava.util.List;5 6 Importorg.jdom.Document;7 Importorg.jdom.Element;8 Importorg.jdom.JDOMException;9 ImportOrg.jdom.input.SAXBuilder;Ten  One  Public classSaxxml { A      -      Public Static voidMain (string[] args) { -         //Parsing XML documents theSaxbuilder Builder =NewSaxbuilder (); -Document docu =NULL; -         Try { -Docu = Builder.build ("E:/io/stu.xml"); +}Catch(jdomexception e) { - e.printstacktrace (); +}Catch(IOException e) { A e.printstacktrace (); at         } -          -Element root = Docu.getrootelement ();//Get root directory -List stulist =Root.getchildren (); -System.out.println (stulist.size () + "-----------"); -SYSTEM.OUT.PRINTLN ("------------------read information about XML documents---------------------"); in          for(inti = 0; I < stulist.size (); i++) { -Element e =(Element) stulist.get (i); to String Stuid; + String Stuname; - String Stusex; the String stuage; *             if(i==0) { $Stuid = E.getchildtext ("Stuid");Panax NotoginsengStuname = E.getchildtext ("Stuname"); -Stusex = E.getchildtext ("Stusex"); theStuage = E.getchildtext ("Stuage"); +}Else { AStuid = E.getchildtext ("Stuid" +i); theStuname = E.getchildtext ("Stuname" +i); +Stusex = E.getchildtext ("Stusex" +i); -Stuage = E.getchildtext ("Stuage" +i);  $             } $System.out.println ("attribute:" +e.getattributevalue ("No")); -System.out.println ("School Number:" +stuid); -System.out.println ("Name:" +stuname); theSystem.out.println ("Age:" +stuage); -System.out.println ("Gender:" +stusex);WuyiSystem.out.println ("--------------------"); the         } -     }     Wu}

Java generates and parses 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.