package com.huawei.it.xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
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.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class ReadWriteXml {
//private static final Logger log = Logger.getLogger(ReadWriteXml.class);
public static final String XML_PATCH="d:/tanchangqing/lib/new.xml";
public static final String XML_PATCH2="d:/tanchangqing/lib/new2.xml";
public void readXml(){
SAXReader saxRead = new SAXReader();
Document document = null;
try {
document = saxRead.read(new File(XML_PATCH));
} catch (DocumentException e) {
// TODO Auto-generated catch block
//log.error("文件不存在,读取失败:"+XML_PATCH, e);
}
Element root = document.getRootElement();
//打印所有节点
// listNodes(root);
Document newXml = createXml();
addAll(document,newXml);
write(document);
}
private Document createXml() {
Document document;
//创建xml
document = DocumentHelper.createDocument();
Element newEle = document.addElement("boot");
Element student = newEle.addElement("student");
Element name = student.addElement("name");
name.addText("tan");
Element age = student.addElement("age");
age.addText("18");
print("新建xml",document.getRootElement());
return document;
}
public void write(Document document){
StringWriter stringWriter = new StringWriter();
OutputFormat xmlFormat = new OutputFormat();
xmlFormat.setEncoding("GBK");
xmlFormat.setNewlines(true);
xmlFormat.setIndent(true);
xmlFormat.setIndent(" ");
// ByteArrayOutputStream out = new ByteArrayOutputStream();
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(XML_PATCH2));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
XMLWriter xmlWriter;
try {
xmlWriter = new XMLWriter(out,xmlFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print("生成的XML",document.getRootElement());
}
public void listNodes(Element node) {
System.out.println("当前节点的名称::" + node.getName());
// 获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println("文本内容::::" + node.getText());
}
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
// 遍历
while (it.hasNext()) {
// 获取某个子节点对象
Element e = it.next();
// 对子节点进行遍历
listNodes(e);
}
}
/**
* 合并xml
* @param element1
* @param element2
* @return
*/
public Document addAll(Document document1,Document document2){
Element root = document1.getRootElement();
Element root2 = document2.getRootElement();
Element next = (Element)root.elementIterator().next();
Element next2 = (Element)root2.elementIterator().next();
if(next.getName().equals(next2.getName())){
List lst = root2.elements();
for(int i=0; i<lst.size(); i++) {
Element element = (Element)lst.get(i);
root.add((Element)element.clone());
}
}
// for(Iterator it = root2.elementIterator(); it.hasNext();) {
// Element element = (Element)it.next();
// root.add(element);
// }
print("合并后的xml",document1.getRootElement());
return document1;
}
public void print(String msg,Element element){
System.out.println(msg+":\n=========="+element.asXML());
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
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.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class ReadWriteXml {
//private static final Logger log = Logger.getLogger(ReadWriteXml.class);
public static final String XML_PATCH="d:/tanchangqing/lib/new.xml";
public static final String XML_PATCH2="d:/tanchangqing/lib/new2.xml";
public void readXml(){
SAXReader saxRead = new SAXReader();
Document document = null;
try {
document = saxRead.read(new File(XML_PATCH));
} catch (DocumentException e) {
// TODO Auto-generated catch block
//log.error("文件不存在,读取失败:"+XML_PATCH, e);
}
Element root = document.getRootElement();
//打印所有节点
// listNodes(root);
Document newXml = createXml();
addAll(document,newXml);
write(document);
}
private Document createXml() {
Document document;
//创建xml
document = DocumentHelper.createDocument();
Element newEle = document.addElement("boot");
Element student = newEle.addElement("student");
Element name = student.addElement("name");
name.addText("tan");
Element age = student.addElement("age");
age.addText("18");
print("新建xml",document.getRootElement());
return document;
}
public void write(Document document){
StringWriter stringWriter = new StringWriter();
OutputFormat xmlFormat = new OutputFormat();
xmlFormat.setEncoding("GBK");
xmlFormat.setNewlines(true);
xmlFormat.setIndent(true);
xmlFormat.setIndent(" ");
// ByteArrayOutputStream out = new ByteArrayOutputStream();
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(XML_PATCH2));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
XMLWriter xmlWriter;
try {
xmlWriter = new XMLWriter(out,xmlFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print("生成的XML",document.getRootElement());
}
public void listNodes(Element node) {
System.out.println("当前节点的名称::" + node.getName());
// 获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println("文本内容::::" + node.getText());
}
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
// 遍历
while (it.hasNext()) {
// 获取某个子节点对象
Element e = it.next();
// 对子节点进行遍历
listNodes(e);
}
}
/**
* 合并xml
* @param element1
* @param element2
* @return
*/
public Document addAll(Document document1,Document document2){
Element root = document1.getRootElement();
Element root2 = document2.getRootElement();
Element next = (Element)root.elementIterator().next();
Element next2 = (Element)root2.elementIterator().next();
if(next.getName().equals(next2.getName())){
List lst = root2.elements();
for(int i=0; i<lst.size(); i++) {
Element element = (Element)lst.get(i);
root.add((Element)element.clone());
}
}
// for(Iterator it = root2.elementIterator(); it.hasNext();) {
// Element element = (Element)it.next();
// root.add(element);
// }
print("合并后的xml",document1.getRootElement());
return document1;
}
public void print(String msg,Element element){
System.out.println(msg+":\n=========="+element.asXML());
}
}