您的位置:首页 > 编程语言 > Java开发

java中利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。

2007-06-04 13:37 1101 查看
前两天,在java项目中写了一些关于对XML文档的操作,利用dom4j对XML文档的创建、解析、查找、修改、保存,校验等操作。以下代码供有需要的朋友参考。

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
/***
* JDK版本:jdk5.0
* 利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。
* @author Jack Chen
* 日期:2007-05-17
*/
public class TestXML {

public static final String SAVE_XMLFILE_PATH = "saveXML.xml";

public TestXML() {

}

/**
* 创建一个XML文档
* @return doc 返回该文档
*/
public Document createXMLDocument(){
Document doc = null;
doc = DocumentHelper.createDocument();
doc.addComment("edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by ()");
// doc.addDocType("class","//By Jack Chen","saveXML.xsd");
Element root = doc.addElement("class");
Element company = root.addElement("company");
Element person = company.addElement("person");
person.addAttribute("id","11");
person.addElement("name").setText("Jack Chen");
person.addElement("sex").setText("男");
person.addElement("date").setText("2001-04-01");
person.addElement("email").setText("chen@163.com");
person.addElement("QQ").setText("2366001");

return doc;
}

/**
* 解析XML文档
* @param xmlFile
* @return XML文档
* @throws DocumentException
* @throws FileNotFoundException
*/
public Document parse(String xmlFile) throws DocumentException, FileNotFoundException{
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(xmlFile));
return doc;
}

/***
* 将XML文档输出到控制台
* @param doc
* @throws IOException
*/
public void printDocument(Document doc) throws IOException{
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new OutputStreamWriter(System.out),format);
writer.write(doc);
writer.close();
}

/**
* 保存XML文档
* @param doc
* @throws IOException
*/
public void saveDocument(Document doc) throws IOException{
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(SAVE_XMLFILE_PATH),format);
writer.write(doc);
writer.close();
}
/**
* 验证XML文档和schemaURL
* @param xmlFile
* @param schemaUrl
* @return XML文档
* @throws SAXException
* @throws DocumentException
*/
public Document validate(String xmlFile,String schemaUrl) throws SAXException, DocumentException{
SAXReader reader = new SAXReader(true);
System.out.println("validate by: " + schemaUrl);

reader.setFeature("http://apache.org/xml/features/validation/schema", true);

reader.setFeature("http://xml.org/sax/features/validation", true);

reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
schemaUrl);

reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
Document doc = reader.read(new File(xmlFile));

return doc;
}

/**
* 查找 xml
* @param doc
* @throws IOException
*/

public void listDocument(Document doc) throws IOException{

String xpath = "/class/company/person[@id=/"11/"]";
Element list = (Element) doc.selectSingleNode(xpath);
System.out.println(list.getName());

// if (list.element("name").getName().equals("name")) {

System.out.println("name:"+list.element("name").getText());
System.out.println("sex:"+list.element("sex").getText());
System.out.println("date:"+list.element("date").getText());
System.out.println("email:"+list.element("email").getText());
System.out.println("QQ:"+list.element("QQ").getText());
}

/***
* 利用XPATH查找元素,然后修改
* @param doc
* @throws IOException
*/
public void updateDocByXPATH(Document doc) throws IOException{
String xpath = "/class/company/person[@id=/"11/"]";
Element list = (Element) doc.selectSingleNode(xpath);
System.out.println(list.getName());

// if (list.element("name").getName().equals("name")) {

list.element("name").setText("1123");
list.element("sex").setText("男");
list.element("date").setText("1800-01-01");
list.element("email").setText("163@163.com");
list.element("QQ").setText("12345");
// }
saveDocument(doc);

}

/**
* 从根节点遍历,来修改XML文件,并保存。
* @param doc
* @throws IOException
*/
public void updateDocument(Document doc) throws IOException{

Element root = doc.getRootElement();
// System.out.println(root.asXML());

for (Iterator i = root.elementIterator(); i.hasNext();) {
Element e = (Element) i.next();
System.out.println(e.getName());
System.out.println(e.getPath());
if(e.element("person").element("name").getName().equals("name")){
e.element("person").element("name").setText("ChenJI");
e.element("person").element("QQ").setText("123456");

}
System.out.println(e.getText().toString());
}
saveDocument(doc);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: