(三)一个简单的例子JDOM的处理方式有些类似于DOM,但它主要是用SAX实现的,你不必担心处理速度和内存的问题。另外,JDOM中几乎没有接口,的类全部是实实在在的类,没有类工厂类的。其最重要的一个包org.jdom中主要有以下类:
Attribute
CDATA
Comment
DocType
Document
Element
EntityRef
Namespace
ProcessingInstruction
Text
数据输入要用到XML文档要通过org.jdom.input包,反过来需要org.jdom.output。如前面所说,关是看API文档就能够使用。
我们的例子读入XML文件exampleA.xml,加入一条处理指令,修改第一本书的价格和作者,并添加一条属性,然后写入文件exampleB.xml:
//exampleA.xml<?xml version="1.0" encoding="GBK"?><bookList> <book> <name>Java编程入门</name> <author>张三</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book> <book> <name>XML在Java中的应用</name> <author>李四</author> <publishDate>2002-9-16</publishDate> <price>92.0</price> </book></bookList>//testJDOM.javaimport org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;public class TestJDOM{ public static void main(String args[])throws Exception{ SAXBuilder sb = new SAXBuilder(); //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了 Document doc = sb.build(new FileInputStream("exampleA.xml")); //加入一条处理指令 ProcessingInstruction pi = new ProcessingInstruction ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\""); doc.addContent(pi); Element root = doc.getRootElement(); //得到根元素 java.util.List books = root.getChildren(); //得到根元素所有子元素的集合 Element book = (Element)books.get(0); //得到第一个book元素 //为第一本书添加一条属性 Attribute a = new Attribute("hot","true"); book.setAttribute(a); Element author = book.getChild("author"); //得到指定的字元素 author.setText("王五"); //将作者改为王五 //或 Text t = new Text("王五");book.addContent(t); Element price = book.getChild("price"); //得到指定的字元素 //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势 author.setText(Float.toString(50.0f)); String indent = " "; boolean newLines = true; XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK"); outp.output(doc, new FileOutputStream("exampleB.xml")); }};执行结果exampleB.xml:
<?xml version="1.0" encoding="GBK"?><bookList> <book hot=”true”> <name>Java编程入门</name> <author>50.0</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book> <book> <name>XML在Java中的应用</name> <author>李四</author> <publishDate>2002-9-16</publishDate> <price>92.0</price> </book></bookList><?xml-stylesheet href="bookList.html.xsl" type="text/xsl"?>======================================================
粘自
http://www.javaeasy.com/ArticleShow.asp?ArticleID=659
作者:bruce 出处:javaresearch
Attribute
CDATA
Comment
DocType
Document
Element
EntityRef
Namespace
ProcessingInstruction
Text
数据输入要用到XML文档要通过org.jdom.input包,反过来需要org.jdom.output。如前面所说,关是看API文档就能够使用。
我们的例子读入XML文件exampleA.xml,加入一条处理指令,修改第一本书的价格和作者,并添加一条属性,然后写入文件exampleB.xml:
//exampleA.xml<?xml version="1.0" encoding="GBK"?><bookList> <book> <name>Java编程入门</name> <author>张三</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book> <book> <name>XML在Java中的应用</name> <author>李四</author> <publishDate>2002-9-16</publishDate> <price>92.0</price> </book></bookList>//testJDOM.javaimport org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;public class TestJDOM{ public static void main(String args[])throws Exception{ SAXBuilder sb = new SAXBuilder(); //从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了 Document doc = sb.build(new FileInputStream("exampleA.xml")); //加入一条处理指令 ProcessingInstruction pi = new ProcessingInstruction ("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\""); doc.addContent(pi); Element root = doc.getRootElement(); //得到根元素 java.util.List books = root.getChildren(); //得到根元素所有子元素的集合 Element book = (Element)books.get(0); //得到第一个book元素 //为第一本书添加一条属性 Attribute a = new Attribute("hot","true"); book.setAttribute(a); Element author = book.getChild("author"); //得到指定的字元素 author.setText("王五"); //将作者改为王五 //或 Text t = new Text("王五");book.addContent(t); Element price = book.getChild("price"); //得到指定的字元素 //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势 author.setText(Float.toString(50.0f)); String indent = " "; boolean newLines = true; XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK"); outp.output(doc, new FileOutputStream("exampleB.xml")); }};执行结果exampleB.xml:
<?xml version="1.0" encoding="GBK"?><bookList> <book hot=”true”> <name>Java编程入门</name> <author>50.0</author> <publishDate>2002-6-6</publishDate> <price>35.0</price> </book> <book> <name>XML在Java中的应用</name> <author>李四</author> <publishDate>2002-9-16</publishDate> <price>92.0</price> </book></bookList><?xml-stylesheet href="bookList.html.xsl" type="text/xsl"?>======================================================
粘自
http://www.javaeasy.com/ArticleShow.asp?ArticleID=659
作者:bruce 出处:javaresearch
格式
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book name="www.126.com">
<site>
<name>Java编程入门</name>
<author>张三</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</site>
</book>
<book name="www.124.com">
<site>
<name>XML在Java中的应用</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</site>
</book>
</bookList>
try {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new FileInputStream("c:\\example.xml"));
Element el = doc.getRootElement();
List ls = el.getChildren("book");
Iterator it = ls.iterator();
while (it.hasNext()) {
Element sub_e = (Element)it.next();
String strAtt = sub_e.getAttribute("name").getValue();
if(strAtt.equals("www.126.com")){
//可以另开一个方法来处理
Element e = sub_e.getChild("name");
//取得节点信息
System.out.println("名称="+e.getName()+" 值="+e.getText());
}else if(strAtt.equals("www.sina.com")){
Element e = sub_e.getChild("name");
//修改节点值
e.setText("修改后的值");
}
}
//添加节点
//el.setChildren();
//删除节点
//el.removeChild("");
XMLOutputter xml = new XMLOutputter("",false, "gb2312");
xml.output(doc, new FileOutputStream("c:\\example.xml"));
}
catch (Exception e) {
System.out.println(e.getMessage()); } }