jdom--powerful and useful XML processing tools

Source: Internet
Author: User
Tags add end getmessage modify string xsl
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.

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.

----------Myjdom.java----------

Import org.jdom.*;
Import org.jdom.input.*;
Import org.jdom.output.*;
Import java.util.*;
Import java.io.*;

public class Myjdom
{

Public Myjdom ()
{
}

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 STUINFO.DTD----------after----------change

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE lit:stuinfo SYSTEM "DTD\STUDENT.DTD" >
<?xml-stylesheet href= "xsl\stuinfo.xsl" type= "text/xsl"?>
<lit:stuinfo xmlns:lit= "http://www.lit.edu.cn/student/" >
<LIT:student>
<LIT:name>bigmouse</LIT:name>
<LIT:sex>male</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>60</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Englist</LIT:lessonName>
<LIT:lessonScore>59</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>autoCAD</LIT:lessonName>
<LIT:lessonScore>100</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>SCM</LIT:lessonName>
<LIT:lessonScore>90</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>mechanics</LIT:lessonName>
<LIT:lessonScore>61</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<lit:breakline/>
<LIT:student>
<LIT:name>coco</LIT:name>
<LIT:sex>female</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>90</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Englist</LIT:lessonName>
<LIT:lessonScore>95</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>C++</LIT:lessonName>
<LIT:lessonScore>80</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>Java</LIT:lessonName>
<LIT:lessonScore>85</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<lit:breakline/>
<LIT:student>
<LIT:name>fannWong</LIT:name>
<LIT:sex>female</LIT:sex>
<LIT:lesson>
<LIT:lessonName>math</LIT:lessonName>
<LIT:lessonScore>85</LIT:lessonScore>
</LIT:lesson>
<LIT:lesson>
<LIT:lessonName>English</LIT:lessonName>
<LIT:lessonScore>95</LIT:lessonScore>
</LIT:lesson>
</LIT:student>
<lit:breakline/>
<LIT:master>
&masterName; </LIT:master>
</LIT:StuInfo>



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.