Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Dom|xml I. INTRODUCTION
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.
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 ();
* Parsing documents
* @param Fileuri
private void Parserxmlfile (String fileuri)
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);
Document doc = Builder.build (Fileuri);
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");
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"));
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 ());
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.