首先我们来看一下dom4j的官方文档,这是最好的方法!
PARSING XML (解析 XML)
One of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in dom4j. The following code demonstrates how to this.
第一件事情,你可能就是想去解析一个xml文档,这是容易的对于dom4j来说。下面的实例就是展示给我们怎么样去做!
try {
//创建一个读取器
SAXReader reader = new SAXReader();
//读取一个xml文档
Document d = reader.read("src/book.xml");
System.out.println("result:"+d.toString());
} catch (DocumentException e) {
throw new RuntimeException(e);
}
USING ITERATORS(运用迭代)
A document can be navigated using a variety of methods that return standard Java Iterators. For example
一个文档可以被操纵用多种方法并且返回一个标准的java迭代器!例如:
//获得一个根节点
Element root = d.getRootElement();
//迭代出根节点的属性,用到了属性迭代器!
for(Iterator<Attribute> it = root.attributeIterator();it.hasNext();)
{
Attribute att = it.next();
System.out.println(att.getName()+"="+att.getValue());
}
//迭代出他的子节点
for(Iterator<Element> it = root.elementIterator();it.hasNext();)
{
Element e = it.next();
System.out.println(e.getName());
}
//迭代出传入参数的元素的节点!
for(Iterator<Element> it = root.elementIterator("file");it.hasNext();)
{
Element e = it.next();
System.out.println(e.getName());
}
FAST LOOPING(快速循环)
If you ever have to walk a large XML document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an Iterator object for each loop. For example
如果你曾不得不创建一个xml的文档树,为了效率,我们提醒你去运用快速循环的方法以适应创建迭代对象的消耗为每次循环。例如:
public void tree(Element root)
{
for(int i = 0,size = root.nodeCount(); i < size ;i++)
{
Node node = root.node(i);
if(node instanceof Element)
{
tree((Element) node);
}
else
{
System.out.println("node:"+node.getText());
}
}
}
CREATE A NEW XML DOCUMENT(创建一个新的xml文档对象)
Often in dom4j you will need to create a new document from scratch. Here's an example of doing that.
经常你需要去创建一个新的文档对象,这就是一个例子:
要用到了DocumentHelper方法。
Document myd = DocumentHelper.createDocument();
在节点或创建的节点上添加节点!
Element root = document.addElement( "root" );
Element author1 = root.addElement( "author" )
.addAttribute( "name", "James" )
.addAttribute( "location", "UK" )
.addText( "James Strachan" );
WRITE DOCUMENT TO FILE(把文件写入文本中)
A quick and easy way to write a Document (or any Node) to a Writer is via the write() method.
一个快速的容易的方法去写入一个文档用write()方法:
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"));
writer.write(root);
writer.close();
If you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with Writer objects or OutputStream objects as the destination, then you can use the XMLWriter class.
如果你想去改变输出的格式,你可以选择是一个漂亮的输出还是一个压实 的输出!当然你也可以写到一个OUTPUTSTREAM中,你都可以用write()
//創建一個漂亮的格式
OutputFormat pre = OutputFormat.createPrettyPrint();
//創建一個壓實的格式
OutputFormat com = OutputFormat.createCompactFormat();
XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"),pre);
writer.write(root);
writer.close();
If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method.
你可以把一个文档对象或者是节点,元素,属性等。变为String用asXML()方法!
Document document = ...; String text = document.asXML();
If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText()。
你一样可以把一个String变为文档或者其他!
String text = "<person> <name>James</name> </person>"; Document document = DocumentHelper.parseText(text);
下一篇我们将讨论xml的乱码问题。