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

前两天,在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);  
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值