Jdom is an API based on JAVA2 that uses Java data types to define the nodes that operate the data tree ... Well, that's it, or see how to use it to parse XML documents.
Two. API
Jdom API is very simple, the number is not much, by looking at the name of the method can know their role. Here is not listed, you need a complete and up-to-date API reference, you can go to the http://www.jdom.org site to view.
Three. Analytic examples
Jdom provides a very convenient and efficient way to manipulate nodes, so it is easy to read and modify XML documents. The following example is to modify Bigmouse's CAD scores and then add a new student profile.
public static void Main (string[] args)
{
if (args.length!= 1)
{
System.out.println ("Usage:java myjdom [XML file URI]");
}
Myjdom myjdom = new Myjdom ();
Myjdom.parserxmlfile (Args[0]);
}
/**
* Parsing documents
* @param Fileuri
*/
private void Parserxmlfile (String fileuri)
{
Try
{
Get an instance of Org.jdom.input.SAXBuilder to get an XML document.
The parameter false indicates no validation.
Parameter does not specify what parser to use, so the default parser is used.
Saxbuilder builder = new Saxbuilder (false);
Get document
Document doc = Builder.build (Fileuri);
Name space
Namespace ns = Namespace.getnamespace ("LIT", "http://www.lit.edu.cn/student/");
Get root node Lit:stuinfo
Element elmtstuinfo = Doc.getrootelement ();
Gets the collection of all lit:student nodes
List lststudents = Elmtstuinfo.getchildren ("student", NS);
Modify the CAD score for Bigmouse
for (int i = 0; i < lststudents.size (); i++)
{
Current student Node
Element elmtstudent = (element) Lststudents.get;
if (Elmtstudent.getchildtexttrim ("name", NS). Equals ("Bigmouse")
{
Collection of all course nodes
List Lstlesson = Elmtstudent.getchildren ("Lesson", NS);
for int j = 0; J < Lstlesson.size (); j + +)
{
Element Elmtlesson = (Element) Lstlesson.get (j);
if (Elmtlesson.getchildtexttrim ("Lessonname", NS). Equals ("AutoCAD")
{
Revise CAD score, 100 min @_@
Elmtlesson.getchild ("Lessonscore", NS). SetText ("100");
SYSTEM.OUT.PRINTLN ("* * autocad:100");
}
}
}//end if
}//end for
Add a student record
Elmtstuinfo.addcontent (new Element ("student", NS). Addcontent (new Element ("name", NS). SetText ("Fannwong")
. Addcontent (New Element ("Sex", NS). SetText ("female")
. Addcontent (new Element ("lesson", NS). Addcontent (new Element ("Lessonname", NS). SetText ("math")
. Addcontent (New Element ("Lessonscore", NS). SetText ("85"))
. Addcontent (new Element ("lesson", NS). Addcontent (new Element ("Lessonname", NS). SetText ("中文版")
. Addcontent (New Element ("Lessonscore", NS). SetText ("95")))
. Addcontent (New Element ("Breakline", NS));
SYSTEM.OUT.PRINTLN ("* * New Element added!");
Adjust the format and put the entity references to the last side
Delete First, append later.
Elmtstuinfo.removechild ("Master", NS);
Elmtstuinfo.addcontent (New Element ("Master", NS). Addcontent (New Entity ("Mastername"));
Output document
The first argument is the indent string, which is 4 spaces.
The second argument is true, which indicates that a line wrap is required.
Xmloutputter printdoc = new Xmloutputter ("", true);
Printdoc.output (Doc, New FileOutputStream ("Stuinfo.xml"));
}
catch (Jdomexception jdome)
{
System.out.println (Jdome.getmessage ());
}
catch (FileNotFoundException Fnfe)
{
System.out.println (Fnfe.getmessage ());
}
catch (IOException IoE)
{
System.out.println (Ioe.getmessage ());
}
}
}
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