之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码
package xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestXml {
public static void main(String[] args) {
createXml("E:/test/human.xml");
try {
readXml("E:/test/human.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建Xml文件
*
* @param fileName
* @return
*/
public static void createXml(String fileName) {
// // 第一种方式:创建文档,并创建根元素
// // 创建文档:使用了一个Helper类
// Document document = DocumentHelper.createDocument();
// // 创建根节点并添加进文档
// Element root = DocumentHelper.createElement("human");
// document.setRootElement(root);
// 第二种方式:创建文档并设置文档的根元素节点
Element root = DocumentHelper.createElement("human");
Document document = DocumentHelper.createDocument(root);
Element name = root.addElement("name");// 添加节点name
name.setText("张三");// 赋值
name.addAttribute("a1", "123");// 添加属性a1,并赋值
Element sex = root.addElement("sex");
sex.setText("男");
try {// 写入文件
// 格式
OutputFormat format = new OutputFormat(" ", true);// 设置缩进为4个空格,并且另起一行为true
Writer fileWriter = new FileWriter(fileName);
XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
xmlWriter.write(document); // 写入文件中
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 解析Xml
*
* @param fileName
*/
public static void readXml(String fileName) throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("E:/test/human.xml"));
// 获取根元素
Element root = document.getRootElement();
System.out.println("Root: " + root.getName());
// 获取所有子元素
List<Element> childList = root.elements();
System.out.println("total child count: " + childList.size());
// 获取特定名称的子元素
List<Element> childList2 = root.elements("name");
System.out.println("name child: " + childList2.size());
// 获取名字为指定名称的第一个子元素
Element firstWorldElement = root.element("name");
// 输出其属性
System.out.println("first World Attr: "
+ firstWorldElement.attribute(0).getName() + "="
+ firstWorldElement.attributeValue("a1"));
System.out.println("迭代输出-----------------------");
// 迭代输出
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element e = (Element) iter.next();
System.out.println(e.getText());
}
System.out.println("用DOMReader-----------------------");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 注意要用完整类名
org.w3c.dom.Document document2 = db.parse(new File("E:/test/human.xml "));
DOMReader domReader = new DOMReader();
// 将JAXP的Document转换为dom4j的Document
Document document3 = domReader.read(document2);
Element rootElement = document3.getRootElement();
System.out.println("Root: " + rootElement.getName());
}
}