dom4j代码示例
package xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class XmlTest {
@Test
public void getDocumentObject() throws DocumentException {
System.out.println("======================读取XML文件,获得document对象====================");
File xmlFile = new File("javaT/src/xml/book.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
System.out.println("======================解析xml文本,获得document对象====================");
String xmlText = "<book></book>";
Document document1 = DocumentHelper.parseText(xmlText);
System.out.println("======================创建document对象====================");
Document document2 = DocumentHelper.createDocument();
Element bookElement = document2.addElement("book");
}
@Test
public void getElement() throws DocumentException {
System.out.println("======================读取XML文件,获得document对象====================");
File xmlFile = new File("src/xml/book.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
System.out.println("======================获取跟节点与子节点以及属性与文本====================");
Element rootElement = document.getRootElement();
Element book1 = rootElement.element("book");
System.out.println("-------------------------------------");
List<Element> books = rootElement.elements("book");
for (Element book : books) {
System.out.println(book);
}
System.out.println("-------------------------------------");
Iterator it = rootElement.elementIterator();
while (it.hasNext()) {
Element next = (Element)it.next();
System.out.println(next);
}
}
@Test
public void elementOperation() throws DocumentException, IOException {
System.out.println("======================获取一个元素,比如根节点元素====================");
File xmlFile = new File("src/xml/book.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
Element rootElement = document.getRootElement();
System.out.println(rootElement.getName());
Element book = rootElement.element("book");
System.out.println("======================获取元素的属性与文本====================");
String id = book.attributeValue("id");
System.out.println(id);
Attribute id1 = book.attribute("id");
String text = id1.getText();
System.out.println(id1.getName());
Element nameElement = book.element("name");
String text1 = nameElement.getText();
System.out.println(text1);
Element countryElement = book.addElement("Country").addAttribute("cid", "0086");
countryElement.setText("China");
XMLWriter writer = new XMLWriter(new FileWriter("newBook.xml"));
writer.write(document);
writer.close();
}
@Test
public void xmlToString() throws DocumentException, IOException {
System.out.println("================字符串转document转xml文件==================");
String text =
"<book id=\"BK001\">\n" +
" <name>Kubernetes</name>\n" +
" <pageCount>800</pageCount>\n" +
" <price>168.00</price>\n" +
" <author>龚正</author>\n" +
" <Country cid=\"0086\">China</Country>\n" +
"</book>";
Document document = DocumentHelper.parseText(text);
XMLWriter writer = new XMLWriter(new FileWriter("newBook1.xml"));
writer.write(document);
writer.close();
System.out.println("================xml文件转字符串==================");
SAXReader reader = new SAXReader();
Document document1 = reader.read(new File("newBook1.xml"));
Element root = document1.getRootElement();
String docXmlText=document1.asXML();
System.out.println(docXmlText + System.lineSeparator());
String rootXmlText=root.asXML();
System.out.println(rootXmlText + System.lineSeparator());
Element memberElm=root.element("name");
String memberXmlText=memberElm.asXML();
System.out.println(memberXmlText);
}
}