package com.company.item.desc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
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;
/** * * @author hongliang.dinghl * Dom4j 生成XML文档与解析XML文档 */
public class Dom4j {
//方法测试
public static void main(String[] args) throws Exception {
Dom4j.createXml("create.xml");
try {
Dom4j.parserXml("create.xml");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 生成XML文档
public static void createXml(String fileName) throws Exception {
Document document = DocumentHelper.createDocument();
Element employees=document.addElement("employees");
Element des=employees.addElement("des");
Element desms= des.addElement("desms");
desms.setText("加密模式为des");
Element key=des.addElement("key");
key.setText("heooodes");
Element desInfo=des.addElement("desInfo");
desInfo.setText("加密内容为4545啊法师打发fdasdf5a4dsf54s5df");
// String ss = document.asXML();
try {
Writer fileWriter = new FileWriter("create.xml");
//xml头文件字符集设定,如果xml内容是什么字节编码则这里最好对应不然解析后内容会出现乱码
org.dom4j.io.OutputFormat format = new OutputFormat();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 解析xml文档
public static void parserXml(String fileName) throws MalformedURLException {
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext();) {
Element employee = (Element) i.next();
for (Iterator j = employee.elementIterator(); j.hasNext();) {
Element node = (Element) j.next();
System.out.println(node.getName() + ":" + node.getText());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
/*********************************************************************************************以下String ss被转换为字节对应的数字来保存字,解析方法改为GBK不会出现乱码/
//生成TXT文件
private static void stf(String ss) throws Exception {
FileOutputStream fos = new FileOutputStream("des.txt");
FileOutputStream fxs = new FileOutputStream("dex.xml");
fos.write(ss.getBytes("GBK"));
fxs.write(ss.getBytes("GBK"));
fos.close();
fxs.close();
}
// 生成xml文档头文件编码格式为UTF-8
private static void xml(String ss) throws Exception {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("DES");
// 添加属性名 DES
root.addAttribute("DES", "DES加密内容");
// 添加节点内容
root.setText(ss);
String xml = doc.asXML();
System.out.println(xml);
FileOutputStream fxs = new FileOutputStream("DES.xml");
fxs.write(xml.getBytes("utf-8"));
fxs.close();
}
// 字符串生成xml文档头文件编码格式为GBK
private static void createXml(String path, String ss) throws IOException {
// 创建一个dom文档对象
Document dom = DocumentHelper.createDocument();
// 为文档添加根节点 des
Element root = dom.addElement("des");
// 为根节点添加属性名称和属性值
root.addAttribute("des", "des加密内容");
// 为根节点添加内容
root.setText(ss);
// 得到一个xml格式的字符串
// String sxml = dom.asXML();
FileWriter fwr = new FileWriter(path);
OutputFormat format = new OutputFormat();
format.setEncoding("GBK");
XMLWriter xmlr = new XMLWriter(fwr, format);
xmlr.write(dom);
xmlr.close();
}
//解析xml文档
private static void parseXml(String path) throws Exception{
File file = new File(path);
SAXReader sr = new SAXReader();
Document dom = sr.read(file);
Element root =dom.getRootElement();
System.out.println("根节点为:"+root.getName()+"*****"+"根节点内容为:"+root.getText());
}