Import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import java.util.List; Import Org.jdom.attribute;import org.jdom.comment;import Org.jdom.document;import Org.jdom.element;import Org.jdom.jdomexception;import Org.jdom.input.saxbuilder;import Org.jdom.output.format;import Org.jdom.output.XMLOutputter; /** * * Jdom Generate and parse XML documents * */public class jdomdemo{Document document = new document (); /** * Use Jdom for writing XML documents */public void Createxml (file file) {//1. Create elements and set as root element elements employ EES = newelement ("Employees"); Document.setcontent (employees); 2. Create a comment and set it on the root element Comment Commet = new Comment ("Thisis my Comment"); Employees.addcontent (Commet); 3. Create elements element element1 = newelement ("employee"); 3.1 Set the attribute name and attribute value of the element Element1.setattribute (Newattribute ("id", "0001")); 3.2 Creating the attribute name and attribute value of the element Attribute nameattr = Newattribute ("Name", "WANGLP"); 3.3 Setting element name and text element Sexele = newelement ("Sex"); Sexele.settext ("M"); Set to upper element element1.addcontent (Sexele); Set element Ageele = Newelement ("Age"); Ageele.settext ("22"); Element1.addcontent (Ageele); Set to the child element of the root element employees.addcontent (element1); Sets the element property to the element Element1.setattribute (nameattr); 3. Create elements element element2 = newelement ("employee"); 3.1 Set the attribute name and attribute value of the element Element2.setattribute (Newattribute ("id", "0002")); 3.2 Create the attribute name and attribute value of the element Attribute name2attr = Newattribute ("name", "Fox"); 3.3 Setting element name and text element Sex2ele = newelement ("Sex"); Sex2ele.settext ("F"); Set to upper element element2.addcontent (Sex2ele); Set element Age2ele = Newelement ("Age"); Age2ele.settext ("21"); Element2.addcontent (Age2ele); Set to the child element of the root element employees.addcontent (Element2); Sets the element property to the element Element2.setattribute (name2attr); Element element3 = new Element ("employee"); Element3.settext ("title"); Element3.addcontent (newelement ("name"). Addcontent (New Element ("Hello")); Employees.addcontent (ELEMENT3); Formats the output of the XML document in format format =format.getprettyformat (); Xmloutputter out = newxmloutputter (format); Output the resulting XML document to the file stream in try {out.output (document, Newfileoutputstream (file)); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }}/** * Use Jdom to read XML documents */public void Parserxml (file file) {//build parser Saxbuilder Bui Lder = new Saxbuilder (); try {//associates parser with document = Builder.build (file); } catch (Jdomexception E1) {e1.printstacktrace (); } catch (IOException E1) {e1.printstacktrace (); }//Read root element Elemental root =document.getrootelement (); The name of the output root element System.out.println ("<" +root.getname () + ">"); Reads a collection of elements list<?> employeelist =root.getchildren ("employee"); for (int i = 0; I <employeelist.size (); i++) {Element ele = (Element) employeelist.get (i); Get the name of the element System.out.println ("<" + ele.getname () + ">"); Reads the attribute collection of the element list<?> empattrlist =ele.getattributes (); for (int j = 0; J <empattrlist.size (); j + +) {Attribute attrs = (Attribute) empattrlist.get (j); Set the name and value of the property and output String name = Attrs.getname (); String value = (string) attrs.getvalue (); SYSTEM.OUT.PRINTLN (name + "=" + value); } try {Element sex =ele.getchild ("sex"); System.out.println ("<sex>" + sex.gettext ()); Element age =Ele.getchild ("Age"); System.out.println ("<age>" + age.gettext ()); } catch (NullPointerException e) {System.out.println (Ele.gettexttrim ()); Element name =ele.getchild ("name"); System.out.println ("<name>" + name.getname ()); } System.out.println ("</employee>"); } System.out.println ("</employees>"); }/** * Test */public static void main (string[] args) {Jdomdemo jdom = new Jdomdemo (); File File = NewFile ("E://jdom.xml"); Jdom.createxml (file); Jdom.parserxml (file); }}
Jdom Generating and parsing XML instances