Jdom模型Q?br />
每个元素都有四个关键D:
1、名U?br /> 2、元素属?br /> 3、元素范围名字空?br /> 4、元素内?/p>
用JDOM处理现有XML文的大致过E如下:
1?用简单无变元构造函数构造一个org.jdom.input.SAXBuilder对象。SAXBuilder用sax解析器从文g中构造文?SAXBuilder侦听sax事gq从内存中徏立一个相应的文。这U方式非常快Q基本上和sax一样快Q,Jdom的速度有值得期待的提高的潜力通过 一个g期的构造器的完成。这个构造器查XML数据源,但当h的时候才对它解析。例如:文档的属性当不访问时是不需要解析的.构造器仍在发展Q可以?sql查询、ldap查询和其他的数据格式来够造Jdom文。所以,一旦进到内存中Q文就和徏造它的工h有关pM?br />
2、用建立器的build()Ҏ从Reader,InputStream,URL,File或包含系lID的字W串建立Document对象?br />
3、如果读取文遇到问题,则抛出IOException,如果建立文遇到问题Q则抛出JDOMException?br />
4、否则用Documentc,Elementcd其他JDOMcȝҎ在文档中建立D?/p>
DJDOM?br />
每个Element对象包含一列子元素QComment,ProcessingInstruction,Text和其他Element对象。此外,属性和其他名字I间q有单独的列表?br />
?析文和建立Document对象之后Q可能要通过搜烦来选择其中E序感兴的部分。在JDOM中,大多数导航通过ElementcȝҎq行。每?Element的完整子元素(包括了子元素的所有内容,包括说明、处理指令、文本节点和元素Q要q行深度搜烦Q就要对当前元素的子元素应用 getContent()ҎQ通常要采用递归)在getContent()Ҏq回java.util.List中提供。getChildren()?法返回的java.util.List中只有每个Element的子元素?br />
JDOM的处理方式有些类gDOMQ但它主要是用SAX实现的,你不必担心处理速度和内存的问题。另外,JDOM中几乎没有接口,的类全部是实实在在的c,没有cd厂类的。其最重要的一个包org.jdom中主要有以下c:
Document(文档节点)
每个Document对象包括下列三个属性:
1、根Element
2、表C文类型声明的DocType对象
3、包含根元素和Q何处理指令与说明的List,按文顺序出?br />
Attribute(属性节?
Public Element setAttributes (List attributes)Throws IllegalAddException
Public List getAttributes()
setAttribute()
getAttribute()
getAttributeValue()
attribute.getName()
attribute.getValue()
CDATA (CDATAD节?
Comment(说明节点)
XML文g的说明:<!-- wire configuration -->
DocType (文档cd节点)
Element(元素节点)
元素名设|和获取
Public Element setName(String name) throws IllegalNameException
Public String getName()
Public String get(int i) //i>=0
Content(内容节点)
Public Element setContent(List list) throws IllegalAddException;
public List getContent();
addContent();
removeContent();
EntityRef(实例节点)
Namespace(名字I间节点)
ProcessingInstruction(处理指o节点)
Text(文本节点)
getText();
setText(String s);
example.xml文g
<?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>
cute.xml文g
<?xml version="1.0" encoding="gb2312"?>
<bookList> //RootElement
<book hot="true"> //<childelement name="value">---->Attribute
<name>Java~程入门</name> //<element>This is my text content</element>
<author>cute</author>
<publishDate>2002-6-6</publishDate>
<price>50.0</price>
</book>
<book>
<name>XML在Java中的应用</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
</bookList>
数据输入要用到XML文要通过org.jdom.input包,反过来需要org.jdom.output。如前面所_x看API文p够用?
我们的例子读入XML文gexample.xmlQ加入一条处理指令,修改W一本书的h格和作者,q添加一条属性,然后写入文gcute.xml
cute.javaQ?/p>
package jdom;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.util.*;
public class Cute {
public static void main(String args[]) {
try {
/*
* 用无变元构造函数构造一个SAXBuilder对象, 用sax解析器从文g中构造文?
* SAXBuilder侦听sax事gq从内存中徏立一个相应的文档
*/
SAXBuilder sb = new SAXBuilder();
// 创徏文
Document doc = sb.build(new FileInputStream("example.xml"));
// 加入一条处理指?br />
ProcessingInstruction pi = new ProcessingInstruction(
"xml-stylesheet",
"href=\"bookList.html.xsl\" type=\"text/xsl\"");
// 把这条处理指令,加入文档?br />
doc.addContent(pi);
// 获得q个文的根元素
Element root = doc.getRootElement();
java.util.List ls = root.getChildren();
// 获得q个根元素的所有子元素(不包含子元素的子元素)Q却完全忽略其内?nbsp;
Iterator i = ls.iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof Text)/*使用instanceof 来获得所需要的内容*/
{Text t=(Text)o;
System.out.println("Text: " + t.getText());}
else if(o instanceof Attribute)
System.out.println("Attribute: " + o);
else if (o instanceof Element)
System.out.println("Element: " + ((Element) o).getName());
}
// 得到W一个子元素的子元素Q却完全忽略其内?br />
Element book = (Element) ls.get(0);
// l这个子元素d一条属性,
Attribute attr = new Attribute("hot", "true");
book.setAttribute(attr);
// 获得q个元素的子元素Q指定)以及其?br />
Element el2 = book.getChild("author");
// 输出q个元素的?br />
System.out.println(el2.getName());
// l这个元素的值改个名?br />
el2.setText("cute");
// 再获得这个元素的子元素(指定Q?br />
Element el3 = book.getChild("price");
// l这个值换个?br />
el3.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter xml = new XMLOutputter(indent, newLines, "gb2312");
xml.output(doc, new FileOutputStream("e:\\cute.xml"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}