//
从文件读取
XML
,输入文件名,返回
XML
文档
public Document read(String fileName) throws MalformedURLException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
return document;
}
|
public Element getRootElement(Document doc){
return doc.getRootElement();
}
|
2.2访问节点
List childrenList = elt.elements();
List childrenList = elt.elements("student");
Element eltChild = elt.element("student");
2.3.访问属性
List attrList = elt.attributes();
Attribute attr = elt.attribute("sn");
String attrValue = elt.attributeValue("sn");
2.4.删除元素和属性
Element eltStu = root.element("student");
root.remove(eltstu);
--------------------------------
etl.remove(elt.attribute("sn"));
3. 遍历XML树
//
枚举所有子节点
for
( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
// do something
}
//
枚举名称为
foo
的节点
for
( Iterator i = root.elementIterator("foo"); i.hasNext();) {
Element foo = (Element) i.next();
// do something
}
//
枚举属性
for
( Iterator i = root.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute) i.next();
// do something
} |
2)递归
public void treeWalk() {
treeWalk(getRootElement());
}
public void treeWalk(Element element) {
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node instanceof Element) {
treeWalk((Element) node);
} else { // do something....
}
}
}
|
public class MyVisitor extends VisitorSupport {
public void visit(Element element){
System.out.println(element.getName());
}
public
void visit(Attribute attr){
System.out.println(attr.getName());
}
} 调用: root.accept(new MyVisitor()) |
public void bar(Document document) {
List list = document.selectNodes( "//foo/bar" );
Node node = document.selectSingleNode("//foo/bar/author");
String name = node.valueOf( "@name" );
}
|
public void findLinks(Document document) throws DocumentException {
List list = document.selectNodes(" //a/@href ");
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Attribute attribute = (Attribute) iter.next();
String url = attribute.getValue();
}
}
|
5. 字符串与XML的转换
// XML转字符串
Document document = ...;
String text = document.asXML();
//
字符串转
XML
String text = "<person> <name>James</name> </person>";
Document document = DocumentHelper.parseText(text);
|
6 用XSLT转换XML
public Document styleDocument(
Document document,
String stylesheet
) throws Exception {
// load the transformer using JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new StreamSource( stylesheet )
);
// now lets style the given document
DocumentSource source = new DocumentSource( document );
DocumentResult result = new DocumentResult();
transformer.transform( source, result );
// return the transformed document
Document transformedDoc = result.getDocument();
return transformedDoc;
}
|
public Document createDocument() {
Document document = DocumentHelper.createDocument();
Element root = document.addElement(root);
Element author1 =
root
.addElement("author")
.addAttribute(name, "James")
.addAttribute("location", "UK")
.addText("James Strachan");
Element author2 =
root
.addElement("author")
.addAttribute("name", Bob")
.addAttribute("location", "US")
.addText("Bob McWhirter");
return document;
}
|
FileWriter
out = new FileWriter( "foo.xml");
document.write(out);
|
如果你想改变输出的格式,比如美化输出或缩减格式,可以用XMLWriter类
public void write(Document document) throws IOException {
//
指定文件
XMLWriter writer = new XMLWriter(
new FileWriter( "output.xml" )
);
writer.write( document );
writer.close();
//
美化格式
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter( System.out, format );
writer.write( document );
//
缩减格式
format = OutputFormat.createCompactFormat();
writer = new XMLWriter( System.out, format );
writer.write( document );
}
|