package com.wuhongbo.common.util.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
* xml写入器
*
* @author wuhongbo
*
*/
public class XmlWrite
{
private Document doc;
private Element root;
/**
* 构造xml写入器
*
* @param xmlFile
* 文件路径
* @throws XmlException
*/
public XmlWrite(String rootNodeName) throws XmlException
{
try
{
doc = DocumentHelper.createDocument();
root = DocumentHelper.createElement("response");
doc.add(root);
}
catch (Exception e)
{
e.printStackTrace();
throw new XmlException("xml创建失败:" + e.getMessage());
}
}
/**
* 创建节点
*
* @param nodeName
* 节点名称
* @param text
* 节点文本,为null时,无内容 如<name/>
* @return 返回节点
*/
public Node createNode(String nodeName, String text)
{
Element node = DocumentHelper.createElement(nodeName);
node.setText(text);
return node;
}
/**
* 增加子节点
*
* @param rootNode
* 根节点
* @param nodeName
* 子节点名称
* @return 返回子节点
* @throws XmlException
*/
public Node addChildNode(Node rootNode, String nodeName)
throws XmlException
{
if (rootNode == null)
{
throw new XmlException("rootNode is null");
}
Element element = (Element) rootNode;
// 子节点
Element node = DocumentHelper.createElement(nodeName);
element.add(node);
return node;
}
/**
* 增加子节点
*
* @param rootNode
* 根节点
* @param nodeName
* 子节点名称
* @param text
* 内容
* @return 返回子节点
* @throws XmlException
*
*/
public Node addChildNode(Node rootNode, String nodeName, String text)
throws XmlException
{
Node node = this.addChildNode(rootNode, nodeName);
node.setText(text);
return node;
}
/**
* 增加子节点
*
* @param rootNode
* 根节点
* @param node
* 子节点
* @return
* @throws XmlException
*/
public Node addChildNode(Node rootNode, Node node) throws XmlException
{
Element element = (Element) rootNode;
element.add(node);
return node;
}
/**
* 增加节点属性
*
* @param rootNode
* 根节点
* @param nodes
* 子节点列表
* @throws XmlException
*/
public void addChildNodes(Node rootNode, List<Node> nodes)
throws XmlException
{
Element element = (Element) rootNode;
if (nodes != null)
{
for (Node node : nodes)
{
element.add(node);
}
}
}
/**
* 设置节点文本
*
* @param node
* 节点
* @param text
* 文本内容
*/
public void setNodeAtt(Node node, String attName, String attValue)
{
if (node != null)
{
Element element = (Element) node;
element.addAttribute(attName, attValue);
}
}
/**
* 获取xml文档根节点
*/
public Node getRootNode()
{
return doc.getRootElement();
}
/**
* 创建xml文件
*
* @param targetFile
* 目录文件
* @throws XmlException
*/
public void createXmlFile(File targetFile) throws XmlException
{
try
{
Writer out = new FileWriter(targetFile);
// 格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint();
// OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
// 创建写出对象
XMLWriter writer = new XMLWriter(out, format);
writer.write(doc);
writer.close();
}
catch (Exception e)
{
throw new XmlException("xml文件生成失败");
}
}
public static void main(String[] args) throws XmlException
{
XmlWrite write = new XmlWrite("response");
Node root =write.getRootNode();
write.setNodeAtt(root,"ver", "1.0");
Node headNode = write.addChildNode(root, "head");
Node bodyNode = write.addChildNode(root, "body");
write.addChildNode(headNode, "status" ,"0");
write.addChildNode(headNode, "message" ,"");
write.addChildNode(bodyNode, "status","0");
write.addChildNode(bodyNode, "message","");
write.addChildNode(bodyNode, "issuccess","false");
File targetFile =new File("c:/test2.xml");
write.createXmlFile(targetFile);
System.out.println("end");
}
public static void main2(String[] args) throws Exception
{
Document doc = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("response");
root.addAttribute("ver", "1.0");
Element head = DocumentHelper.createElement("head");
Node status = DocumentHelper.createElement("status");
status.setText("1<abc>2</abc>");
head.add(status);
Element body = DocumentHelper.createElement("body");
root.add(head);
root.add(body);
doc.add(root);
System.out.println(doc.getText());
// Writer out = new FileWriter("xml/emps.xml");
// out.write(doc.getText());
try
{
Writer out = new FileWriter("c:/test2.xml");
// 格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint();
// OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
// 创建写出对象
XMLWriter writer = new XMLWriter(out, format);
writer.write(doc);
writer.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了。");
}
System.out.println("end");
}
}
package com.wuhongbo.common.util.xml;
import java.io.File;
import java.util.ArrayList;
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.Node;
import org.dom4j.io.SAXReader;
/**
* xml读取器
*
* @author wuhongbo
*
*/
public class XmlRead
{
private SAXReader reader;
private Document doc;
/**
* 构造xml解析器
*
* @param xmlFile
* 文件路径
* @throws XmlException
*/
public XmlRead(File xmlFile) throws XmlException
{
try
{
reader = new SAXReader();
doc = reader.read(xmlFile);
}
catch (DocumentException e)
{
throw new XmlException("xml创建失败:" + e.getMessage());
}
}
/**
* 构造xml解析器
*
* @param xmlStr
* xml内容
* @throws XmlException
*/
public XmlRead(String xmlStr) throws XmlException
{
try
{
doc = DocumentHelper.parseText(xmlStr);
}
catch (DocumentException e)
{
throw new XmlException("xml创建失败:" + e.getMessage());
}
}
/**
* 获取根节点
*
* @throws XmlException
*/
public Node getRootNode()
{
return doc.getRootElement();
}
/**
* 获取指定节点下的唯一节点
*
* @param rootNode
* 指定根节点,不为空
* @param nodeName
* 获取节点的名称
* @return 返回节点,无则返回为空,有多个则返回第1个
* @throws XmlException
*/
public Node getNode(Node rootNode, String nodeName) throws XmlException
{
if (rootNode == null)
{
// throw new XmlException("rootNode is null");
return null;
}
return rootNode.selectSingleNode(nodeName);
}
/**
* 获取指定节点下的节点
*
* @param rootNode
* 指定根节点
* @param nodeName
* 获取节点的名称
* @return 返回节点,无则返回为空,有多个则返回第1个
* @throws XmlException
*/
public List<Node> getNodes(Node rootNode, String nodeName)
throws XmlException
{
if (rootNode == null)
{
// throw new XmlException("rootNode is null");
return new ArrayList<Node>();
}
List<Node> list = rootNode.selectNodes(nodeName);
return list;
}
/**
* 获取节点的指定属性的值
*
* @param node
* 指根节点,不为空
* @param attName
* 获取属性的名称
* @return 返回节点,无则返回为空
* @throws XmlException
*/
public String getNodeAtt(Node node, String attName) throws XmlException
{
if (node == null)
{
return null;
}
Element element = (Element) node;
return element.attributeValue(attName);
}
/**
* 获取指定根节点下的唯一节点的文本
*
* @param root
* 指定根节点,不为空
* @param nodeName
* 获取节点的名称
* @return 返回节点,无则返回为空,有多个则返回第1个
* @throws XmlException
*/
public String getNodeText(Node rootNode, String nodeName)
throws XmlException
{
if (rootNode == null)
{
// throw new XmlException("rootNode is null");
return null;
}
Node node = rootNode.selectSingleNode(nodeName);
if (node != null)
{
return node.getStringValue();
}
return null;
}
/**
* 读取测试例子
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
String xmlStr = "";
File xmlFile = new File("c:/test2.xml");
XmlRead xmlRead = new XmlRead(xmlFile);
Node rootNode = xmlRead.getRootNode();
System.out.println(xmlRead.getNodeAtt(rootNode, "ver"));
Node headNode = xmlRead.getNode(rootNode, "head");
String status = xmlRead.getNodeText(headNode, "status");
System.out.println("status:" + status);
Node bodyNode = xmlRead.getNode(rootNode, "body");
Node fileListNode = xmlRead.getNode(bodyNode, "filelist");
List<Node> list = xmlRead.getNodes(fileListNode, "file");
for (Node node : list)
{
System.out.println("----------------------");
System.out.println(xmlRead.getNodeText(node, "id"));
System.out.println(xmlRead.getNodeText(node, "name"));
}
System.out.println("end");
}
}