A jar package that needs to be imported into the jdom;
1. Jdom Create XML
Package com.shengsiyuan.jdom;
Import Java.io.FileWriter;
Import Org.jdom.Attribute;
Import org.jdom.Comment;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.output.Format;
Import Org.jdom.output.XMLOutputter;
Public class JDomTest1
{
public static void Main (string[] args) throws Exception
{
 &N bsp; //Create documents
document document = new doc ();
& nbsp; //Create document root element
element root = new Element ("root");
//To document Add root element
document.addcontent (root);
//add Comment
Comment Comment = new Comment ("This was my comments");
//Add comment under root element
root.addcontent (comment);
//Create an element
element e = new Element ("Hello");
//The first way to add attributes to an element, which has a return value, returns an Element object
e.setattribute ("Sohu", " Www.sohu.com ");
//Add attributes to the root element
root.addcontent (e);
element e2 = new Element ("World");
//The second way to create element properties
attribute attr = new Attribute ("test") "hehe");
//Adding attributes to the element
e2.setattribute (attr);
//Adding elements to the root element under
e.addcontent (E2);
//Add element attributes and text content, which belongs to the programmatic style of the method chain
e2.addcontent (new Element ("AAA"). setattribute ("A", "B")
.setattribute ("x", "Y"). setattribute ("GG", "hh"). SetText ("text content");
Set its format, there is generally a format without indentation and space Getrawformat, mainly for the network transport of XML, because it has no space, reduce the amount of data transmitted by the network.
Format format = Format.getprettyformat ();
Set the front space of an element
Format.setindent ("");
Set encoding mode
Format.setencoding ("GBK");
outputting an XML document
Xmloutputter out = new Xmloutputter (format);
Out.output (document, New FileWriter ("Jdom.xml"));
}
}
2. Jdom Parsing xml
Package com.shengsiyuan.jdom;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.util.List;
Import Org.jdom.Attribute;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;
Import Org.jdom.output.Format;
Import Org.jdom.output.XMLOutputter;
public class JDomTest2
{
public static void Main (string[] args) throws Exception
{
Jdom parsing from XML
Saxbuilder builder = new Saxbuilder ();
Get XML Document Object
Document doc = builder.build (new File ("Jdom.xml"));
Get the document root element
Element element = Doc.getrootelement ();
System.out.println (Element.getname ());
Get the Hello element under the root element
Element Hello = element.getchild ("Hello");
System.out.println (Hello.gettext ());
Get the attributes under the Hello child element
List List = Hello.getattributes ();
Get the name and value of the attribute under the Hello element
for (int i = 0; i < list.size (); i++)
{
Attribute attr = (attribute) list.get (i);
String attrname = Attr.getname ();
String AttrValue = Attr.getvalue ();
System.out.println (attrname + "=" + AttrValue);
}
Delete the world child element under Hello
Hello.removechild ("World");
Save a document to another file
Xmloutputter out = new Xmloutputter (Format.getprettyformat (). SetIndent (""));
Out.output (Doc, New FileOutputStream ("Jdom2.xml"));
}
}
3. Comprehensive example
Package cn.com.xmltest;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.List;
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;
public class Xmltest
{
public static void Main (string[] args)
{
Modifyxml ();
}
public static void Createxml ()
{
element root = new Element ("Student Information");
Element stu1 = new Element ("student");
Element stu2 = new Element ("student");
Element name1 = new Element ("name");
Element sex1 = new Element ("gender");
Element age1 = new Element ("age");
Element name2 = new Element ("name");
Element sex2 = new Element ("gender");
Element age2 = new Element ("age");
Name1.settext ("Xiaoming");
Sex1.settext ("male");
Age1.settext ("19");
Name2.settext ("Floret");
Sex2.settext ("female");
Age2.settext ("20");
Root.addcontent (STU1);
Root.addcontent (STU2);
Stu1.addcontent (NAME1);
Stu1.addcontent (SEX1);
Stu1.addcontent (AGE1);
Stu2.addcontent (name2);
Stu2.addcontent (SEX2);
Stu2.addcontent (Age2);
Document doc = new document (root);
Format f = Format.getprettyformat ();
F.setencoding ("gb2312");
Xmloutputter out = new Xmloutputter (f);
Try
{
Out.output (Doc, New FileOutputStream (New File ("C:/myxml.xml"));
}
catch (FileNotFoundException e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
public static void Searchxml ()
{
Saxbuilder build = new Saxbuilder ();
Try
{
Document doc = Build.build ("C:/myxml.xml");
Element e = Doc.getrootelement ();
list<element> list = E.getchildren ();
for (Element ele:list)
{
System.out.println (Ele.getchild ("name"). GetName () + ":"
+ ele.getchild ("name"). GetText ());
System.out.println (Ele.getchild ("Gender"). GetName () + ":"
+ Ele.getchild ("gender"). GetText ());
System.out.println (Ele.getchild ("Age"). GetName () + ":"
+ Ele.getchild ("Age"). GetText ());
}
}
catch (Jdomexception e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
public static void AddElement ()
{
Saxbuilder build=new Saxbuilder ();
Try
{
Document doc=build.build ("C:/myxml.xml");
Element root=doc.getrootelement ();
Element stu=root.getchild ("student");
Element cla=new Element ("class");
Cla.addcontent ("Class 2");
Stu.addcontent (CLA);
Xmloutputter out=new Xmloutputter ();
Out.output (Doc,new FileOutputStream) (New File ("C:/myxml.xml"));
}
catch (Jdomexception e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
public static void Modifyxml ()
{
Saxbuilder build=new Saxbuilder ();
Document Doc;
Try
{
doc = Build.build ("C:/myxml.xml");
Element root=doc.getrootelement ();
Element stu=root.getchild ("student");
Element Age=stu.getchild ("Age 2");
Age.setname ("Age");
Age.settext ("10");
List<element> List=stu.getchildren ();
for (Element e:list)
{
if (E.getname (). Equals (""));
// {
Element height=new Element ("height");
Height.settext ("175 cm");
Root.addcontent (height);
Stu.removechild (str);
// }
}
Xmloutputter out=new Xmloutputter ();
Out.output (Doc,new FileOutputStream) (New File ("C:/myxml.xml"));
}
catch (Jdomexception e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}