`

Dom4j的使用

阅读更多
Dom4j的使用 [原创 2007-01-20 12:36:26]     字号:大 中 小 package net.bingosoft.util.xmlparse;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
* 调用dom4j包解析xml文件
*
* @author bobo
*
*/
public class XMLHelper {

final private static Log log = LogFactory.getLog(XMLHelper.class);
/**
  * 根据xml文件的相对路径获取doc对象
  *
  * @param xmlPath
  * @return
  */
public static Document readXML(String xmlPath) {
  Document doc = null;
  SAXReader saxReader = new SAXReader();
  InputStream ins = XMLHelper.class.getClassLoader().getResourceAsStream(
    xmlPath);
  try {
   doc = saxReader.read(ins);
  } catch (DocumentException e) {
   e.printStackTrace();
  }
  return doc;
}

/**
  * 将xml字符串转换为doc对象
  *
  * @param xmlString
  * @return
  */
public static Document readXMLContentString(String xmlString) {
  Document doc = null;
  try {
   doc = DocumentHelper.parseText(xmlString);
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return doc;
}

/**
  * 测试写xml对象到文件
  *
  * @param fileName
  * @return
  */
public static Document createXML(String fileName) {

  Document doc = DocumentHelper.createDocument();
  Element root = doc.addElement("book");
  root.addAttribute("name", "我的图书");
  Element childTmp;
  childTmp = root.addElement("price");
  childTmp.setText("21.22");
  Element writer = root.addElement("author");
  writer.setText("李四");
  writer.addAttribute("ID", "001");
  try {
   // 注意这里的修改
   XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(fileName));
   xmlWriter.write(doc);
   xmlWriter.close();
  } catch (Exception e) {
   System.out.println(e);
  }
  return doc;
}

/**
  * 获得一个指定了根节点名称的doc对象
  *
  * @param rootElementName
  * @return
  */
public static Document createRootElementXML(String rootElementName) {
  Document doc = DocumentHelper.createDocument();
  doc.addElement(rootElementName);
  return doc;
}

/**
  * 获得一个doc对象的根节点
  *
  * @param doc
  * @return
  */
public static Element getRootElement(Document doc) {
  if (doc != null) {
   return doc.getRootElement();
  } else {
   return null;
  }
}

/**
  * 向doc的根节点新增子节点
  *
  * @param doc
  * @param nodeName
  * @param nodeValue
  * @return
  */
public static Document appendElementToRoot(Document doc, String nodeName,
   String nodeValue) {
  if (doc != null && nodeName != null) {
   Element root = doc.getRootElement();
   Element childTmp = root.addElement(nodeName);
   if(nodeValue!=null){
   childTmp.setText(nodeValue);
   }
  }
  return doc;
}

/**
  * 往节点中新增子节点
  *
  * @param parentElement
  * @return
  */
public static Element appendElement(Element parentElement, String nodeName,
   String nodeValue) {
  if (parentElement != null && nodeName != null) {
   Element childTmp = parentElement.addElement(nodeName);
   if(nodeValue!=null){
   childTmp.setText(nodeValue);
   }
  }
  return parentElement;
}

/**
  * 从来源doc中获取指定的节点追加到目标doc的根节点下
  *
  * @param toDoc
  * @param fromDoc
  * @param nodeName
  * @return
  */
public static Document appendElementFromOtherDoc(Document toDoc,
   Document fromDoc, String nodeName) {
  if (toDoc != null && fromDoc != null && nodeName != null) {
   String nodeValue = getNodeText(fromDoc, nodeName);
   Element root = toDoc.getRootElement();
   appendElement(root, nodeName, nodeValue);
  }
  return toDoc;
}

/**
  * 获取dom中指定节点的内容
  *
  * @param doc
  * @param nodeName
  * @return
  */
public static String getNodeText(Document doc, String nodeName) {
  String xpath = "//" + nodeName;
  String nodeText = "";
  try {
   Node node = doc.selectSingleNode(xpath);
   if (node != null)
    nodeText = node.getText();
  } catch (Exception e) {
   log.error("get exception", e);
  }
  return nodeText;
}

/**
  * 从一个xml字符串中获取节点内容
  *
  * @param docXmlStr
  * @param nodeName
  * @return
  */
public static String getNodeText(String docXmlStr, String nodeName) {
  Document doc = null;
  try {
   doc = DocumentHelper.parseText(docXmlStr);
  } catch (DocumentException e1) {
   log.error("error",e1);
  } 
  String nodeText = "";
  if (doc != null) {  
    nodeText = getNodeText(doc,nodeName);  
   }
  return nodeText;
}

/**
  * 校验xml文档中是否含有指定的节点
  *
  * @param doc
  * @param nodeName
  * @return
  */
public static boolean checkIfExistNode(Document doc, String nodeName) {
  boolean flag = false;
  List list = doc.selectNodes("//" + nodeName);
  if (list != null && list.size() > 0) {
   flag = true;
  }
  return flag;
}

// /**
//  * @param args
//  */
// public static void main(String[] args) {
//  // // TODO Auto-generated method stub
//  // Document doc=XMLHelper.createXML("test.xml");
//  // // doc=XMLHelper.readXML("payment.xml");
//  //  
//  // String xmlContent = "<test>中文</test>";
//  // doc=XMLHelper.readXMLContentString(xmlContent);
//  // System.out.println(doc.asXML());
//  // //
//  // //
//  // System.out.println("price:"+doc.selectSingleNode("//book[@name=\"我的图书\"]/price").getText());
//  // //
//  // System.out.println("price:"+((Node)doc.selectNodes("//price").get(0)).getText());
//  // //
//  Document doc = XMLHelper.createRootElementXML("Root");
//  XMLHelper.appendElementToRoot(doc, "name", "bobo");
//  XMLHelper.appendElement(XMLHelper.getRootElement(doc), "age", null);
//  System.out.println(doc.asXML());
//
//  Document doc2 = XMLHelper.createRootElementXML("Root2");
//  XMLHelper.appendElementToRoot(doc2, "name2", "bobo2");
//  XMLHelper.appendElementFromOtherDoc(doc2, doc, "age");
//  System.out.println(doc2.asXML());
// }

}
分享到:
评论

相关推荐

    dom4j使用手册

    dom4j使用手册;

    DOM4J使用详解

    DOM4J使用详解包括具体的增加、删除、获取节点、属性

    dom4j使用教程

    dom4j使用教程 感觉很好 与大家共享

    XSD使用dom4j校验XML

    XSD使用dom4j校验XML

    Dom4j使用简介

    Dom4j使用简介,日志 开发java必看,初级高级开发必看

    DOM4J jar包 xml解析 所有的dom4j-1.6.1 dom4j-2.0.2 dom4j-2.1.1包 导入直接使用

    DOM4J jar包 所有的包 xml解析 dom4j-1.6.1 dom4j-2.0.2 dom4j-2.1.1 导入直接使用

    Dom4j 使用指南.rar

    Dom4j 使用指南 对学习xml的用户有很大的帮助

    Dom4J使用手册和说明

    Dom4J使用手册和说明 ,方便有需要的人参考和使用。 Web开发,Java开发。。。。

    DOM4J使用简介(很实用)

    DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the ...DOM4J使用起来非常简单。

    dom4j使用教程+dom4j.jar

    收集于网络的dom4j解析使用,包括创建,修改,删除等并且有jar文件一同上传

    Dom4j的使用(全而好的文章)

    Dom4j的使用(全而好的文章),不看后悔哦。刚学习过,灰常好的资源。

    dom4j dom4j dom4j dom4j

    dom4j dom4j dom4j dom4j dom4j dom4j

    dom4j使用简介

    Dom4j 是一个易用的、开源的库,用于 XML,XPath 和 XSLT。它应用于 Java 平 台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP

    DOM4J的jar包和API

    与JDOM不同的是,dom4j使用接口和抽象基类,虽然Dom4j的API相对要复杂一些,但它提供了比JDOM更好的灵活性。 Dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极易使用的特点。现在很多软件采用的Dom4...

    Dom4j 使用简介

    Dom4j 使用简介,Dom4j 使用简介

    dom4j的使用教程

     DOM4J使用起来非常简单。只要你了解基本的XML-DOM模型,就能使用。然而他自己带的指南只有短短一页(html),不过说的到挺全。国内的中文资料很少。因而俺写这个短小的教程方便大家使用,这篇文章仅谈及基本的用法...

    dom4j使用介绍

    里面有dom4j的相关资料,还有一些dom4j的相关接口、属性和方法,需要的拿去看看吧

    dom4j-2.1.1-API文档-中英对照版.zip

    赠送jar包:dom4j-2.1.1.jar; 赠送原API文档:dom4j-2.1.1-javadoc.jar; 赠送源代码:dom4j-2.1.1-sources.jar; 赠送Maven依赖信息文件:dom4j-2.1.1.pom; 包含翻译后的API文档:dom4j-2.1.1-javadoc-API文档-...

    dom4j 简单教程

    本人总结的dom4j使用方法,希望能帮助刚刚接触的朋友。

Global site tag (gtag.js) - Google Analytics