jdom的包结构:
jdom的数据流
解析XML文件的基本操作:
1 由XML文件建立Document模型
构建过程主要包括org.jdom.input.SAXBuider和org.jdom.input.DOMBuilder两个类。其中,SAXBuilder使用SAX parser产生XML文件,而DOMBuilder可用来读取DOM tree。
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(fileName);
2 获取根节点
Element rootElement = doc.getRootElement();
3 获取子节点
// Get a List of direct children as Elements
List allChildren = element.getChildren();
List allChildren = element.getChildren();
// Get all direct children with a given name
List namedChildren = element.getChildren("name");
List namedChildren = element.getChildren("name");
// Get the first kid with a given name
Element kid = element.getchild("name");
Element kid = element.getchild("name");
4 增加和删除子节点
添加子节点
// Add a new child
allChildren.add(new Element("jane"));
element.addContent(new Element("jane"));
// Add a new child in the second position
allChildren.add(1, new Element("second"));
allChildren.add(new Element("jane"));
element.addContent(new Element("jane"));
// Add a new child in the second position
allChildren.add(1, new Element("second"));
删除子节点
List allChildren = element.getChildren();
// Remove the fourth child
allChildren.remove(3);
// Remove all children named "jack"
allChildren.removeAll(element.getChildren("jack"));
element.removeChildren("jack");
// Remove the fourth child
allChildren.remove(3);
// Remove all children named "jack"
allChildren.removeAll(element.getChildren("jack"));
element.removeChildren("jack");
5 读取和设置节点属性
读取节点属性
例如:Element table
<table width="100%" border="0"> </table>
String value =table.getAttributeValue("width");
设置节点属性
// Add an attribute
table.setAttribute("vspace", "0");
// Add an attribute more formally
table.setAttribute(new Attribute("name", "value"))
// Remove an attribute
table.removeAttribute("border");
// Remove all attributes
table.getAttributes().clear();
table.setAttribute("vspace", "0");
// Add an attribute more formally
table.setAttribute(new Attribute("name", "value"))
// Remove an attribute
table.removeAttribute("border");
// Remove all attributes
table.getAttributes().clear();
6 读取和设置元素内容
读取元素内容
String content = element.getText();
设置元素内容
// This blows away all current content
element.setText("A new description");
element.setText("A new description");
element.setText("<xml> content");
element.addContent(new CDATA("<xml> content"));
7 关于混合内容
一个节点(element)有时候会同时包含文字内容(text)、子节点(child element)和注释(comments)。例如:Element table
<table>
<!-- Some comment -->
Some text
<tr>Some child</tr>
</table>
获得table的内容和子节点
String text = table.getTextTrim();
Element tr = table.getChild("tr");
获得所有混合内容
List mixedContent = table.getMixedContent();
Iterator i = mixedContent.iterator();
while(i.hasNext) {
Object o = i.next();
if(o instanceof Comment) {
//This is a comment that has a toString()
...
} else if(o instanceof String) {
//This is the text
...
} else if(o instanceof Element) {
//This is the element
...
}
}
Iterator i = mixedContent.iterator();
while(i.hasNext) {
Object o = i.next();
if(o instanceof Comment) {
//This is a comment that has a toString()
...
} else if(o instanceof String) {
//This is the text
...
} else if(o instanceof Element) {
//This is the element
...
}
}
(PS: The above is from jdom documents.)
下面给出一个创建XML文件的代码:
/** */
/**
* 创建XML文件
*/
public void createXML(String filename) ... {
try ...{
Document doc = new Document();//新建XML文件
/** *//**
* 创建PI,写入文件
*/
Map<String, String> map = new HashMap<String, String>();
map.put("type", "text/xsl");
map.put("href", "elements.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", map);
doc.addContent(pi);
/** *//**
* 创建文件类型,写入文件
*/
DocType docType = new DocType("RootElement");
docType.setPublicID("public.dtd");
//docType.setSystemID("system.dtd");
doc.addContent(docType);
/** *//**
* 创建文件内容,写入文件
*/
Element root = new Element("RootElement");
doc.setRootElement(root);
Element child = new Element("ChildElement");
root.addContent(child);
Attribute attribute = new Attribute("ElementID","01");
child.setAttribute(attribute);
child.addContent(new Element("GrandChild1").setText("GC1name"));
child.addContent(new Element("GrandChild2").setText("GC2name"));
/** *//**
* 输出到XML文件
*/
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();//格式化
format.setEncoding("GBK");
outputter.setFormat(format);
outputter.output(doc, new FileOutputStream(new File(filename)));//输出文件
} catch(Exception e) ...{
e.printStackTrace();
}
}
* 创建XML文件
*/
public void createXML(String filename) ... {
try ...{
Document doc = new Document();//新建XML文件
/** *//**
* 创建PI,写入文件
*/
Map<String, String> map = new HashMap<String, String>();
map.put("type", "text/xsl");
map.put("href", "elements.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", map);
doc.addContent(pi);
/** *//**
* 创建文件类型,写入文件
*/
DocType docType = new DocType("RootElement");
docType.setPublicID("public.dtd");
//docType.setSystemID("system.dtd");
doc.addContent(docType);
/** *//**
* 创建文件内容,写入文件
*/
Element root = new Element("RootElement");
doc.setRootElement(root);
Element child = new Element("ChildElement");
root.addContent(child);
Attribute attribute = new Attribute("ElementID","01");
child.setAttribute(attribute);
child.addContent(new Element("GrandChild1").setText("GC1name"));
child.addContent(new Element("GrandChild2").setText("GC2name"));
/** *//**
* 输出到XML文件
*/
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();//格式化
format.setEncoding("GBK");
outputter.setFormat(format);
outputter.output(doc, new FileOutputStream(new File(filename)));//输出文件
} catch(Exception e) ...{
e.printStackTrace();
}
}