DOM4J
/**
* doc2XmlFile 将Document对象保存为一个xml文件到本地
*
* @return true:保存成功 flase:失败
* @param filename
* 保存的文件名
* @param document
* 需要保存的document对象
*/
public static boolean doc2XmlFile(Document document, String filename) {
boolean flag = true;
try {
/* 将document中的内容写入文件中 */
// 默认为UTF-8格式,指定为"GB2312"
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter writer = new XMLWriter(
new FileWriter(new File(filename)), format);
writer.write(document);
writer.close();
} catch (Exception ex) {
flag = false;
ex.printStackTrace();
}
return flag;
}
/**
* string2XmlFile 将xml格式的字符串保存为本地文件,如果字符串格式不符合xml规则,则返回失败
*
* @return true:保存成功 flase:失败
* @param filename
* 保存的文件名
* @param str
* 需要保存的字符串
*/
public static boolean string2XmlFile(String str, String filename) {
boolean flag = true;
try {
Document doc = DocumentHelper.parseText(str);
flag = doc2XmlFile(doc, filename);
} catch (Exception ex) {
flag = false;
ex.printStackTrace();
}
return flag;
}
/**
* load 载入一个xml文档
*
* @return 成功返回Document对象,失败返回null
* @param uri
* 文件路径
*/
public static Document load(String filename) {
Document document = null;
try {
SAXReader saxReader = new SAXReader();
document = saxReader.read(new File(filename));
} catch (Exception ex) {
ex.printStackTrace();
}
return document;
}
/**
* xmlWriteDemoByString 演示String保存为xml文件
*/
public void xmlWriteDemoByString() {
String s = "";
/** xml格式标题 "<?xml version='1.0' encoding='GB2312'?>" 可以不用写 */
s = "<config>\r\n" + " <ftp name='DongDian'>\r\n"
+ " <ftp-host>127.0.0.1</ftp-host>\r\n"
+ " <ftp-port>21</ftp-port>\r\n"
+ " <ftp-user>cxl</ftp-user>\r\n"
+ " <ftp-pwd>longshine</ftp-pwd>\r\n"
+ " <!-- ftp最多尝试连接次数 -->\r\n"
+ " <ftp-try>50</ftp-try>\r\n"
+ " <!-- ftp尝试连接延迟时间 -->\r\n"
+ " <ftp-delay>10</ftp-delay>\r\n" + " </ftp>\r\n"
+ "</config>\r\n";
// 将文件生成到classes文件夹所在的目录里
string2XmlFile(s, "d://xmlWriteDemoByString.xml");
// 将文件生成到classes文件夹里
// string2XmlFile(s, "classes/xmlWriteDemoByString.xml");
}
/**
* 演示手动创建一个Document,并保存为XML文件
*/
public void xmlWriteDemoByDocument() {
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立config根节点 */
Element configElement = document.addElement("config");
/** 建立ftp节点 */
configElement.addComment("东电ftp配置");
Element ftpElement = configElement.addElement("ftp");
ftpElement.addAttribute("name", "DongDian");
/** ftp 属性配置 */
Element hostElement = ftpElement.addElement("ftp-host");
hostElement.setText("127.0.0.1");
(ftpElement.addElement("ftp-port")).setText("21");
(ftpElement.addElement("ftp-user")).setText("cxl");
(ftpElement.addElement("ftp-pwd")).setText("longshine");
ftpElement.addComment("ftp最多尝试连接次数");
(ftpElement.addElement("ftp-try")).setText("50");
ftpElement.addComment("ftp尝试连接延迟时间");
(ftpElement.addElement("ftp-delay")).setText("10");
/** 保存Document */
doc2XmlFile(document, "d://xmlWriteDemoByDocument.xml");
}
/**
* 演示读取文件的具体某个节点的值
*/
public static void xmlReadDemo() {
Document doc = load("d://xmlWriteDemoByDocument.xml");
// Element root = doc.getRootElement();
/** 先用xpath查找所有ftp节点 并输出它的name属性值 */
List list = doc.selectNodes("/config/ftp");
Iterator it = list.iterator();
while (it.hasNext()) {
Element ftpElement = (Element) it.next();
System.out.println("ftp_name="
+ ftpElement.attribute("name").getValue());
}
/** 直接用属性path取得name值 */
list = doc.selectNodes("/config/ftp/@name");
it = list.iterator();
while (it.hasNext()) {
Attribute attribute = (Attribute) it.next();
System.out.println("@name=" + attribute.getValue());
}
/** 直接取得DongDian ftp的 ftp-host 的值 */
list = doc.selectNodes("/config/ftp/ftp-host");
it = list.iterator();
Element hostElement = (Element) it.next();
System.out.println("DongDian's ftp_host=" + hostElement.getText());
}
/**
* doc2String 将xml文档内容转为String
*
* @return 字符串
* @param document
*/
public static String doc2String(Document document) {
String s = "";
try {
// 使用输出流来进行转化
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用GB2312编码
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("UTF-8");
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
实例一:(读取硬盘存在的xml文件)
SAXReader read=new SAXReader();
Document doc=null;
try {
doc=read.read(new File("d://student.xml"));
List classes=doc.selectNodes("//class");
System.out.println("某学校共有"+classes.size()+"个班级");
for(Iterator it=classes.iterator();it.hasNext();){
Element eleClass=(Element)it.next();
String id=eleClass.attributeValue("name");
System.out.println("班级 "+id);
System.out.println("***********************");
Iterator it1=eleClass.elementIterator();
while(it1.hasNext()){
Element eleStu=(Element)it1.next();
String name=eleStu.elementText("name");
String age=eleStu.element("age").getTextTrim();
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
实例二:(读取xml字符串)
StringBuffer sb=new StringBuffer("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
sb.append("<result>");
sb.append("<class name=\"1\">");
sb.append("<student id=\"1\">");
sb.append("<name>龙准</name>");
sb.append("<age>25</age>");
sb.append("</student>");
sb.append("<student id=\"2\">");
sb.append("<name>龙准2</name>");
sb.append("<age>23</age>");
sb.append("</student>");
sb.append("</class>");
sb.append("</result>");
try {
//第一种读取xml字符串的写法
Document doc=DocumentHelper.parseText(sb.toString());
//第二种读取xml字符串的写法
// SAXReader read=new SAXReader();
// Document doc=read.read(new StringReader(sb.toString()));
//第三种读取xml字符串的写法
// SAXReader read=new SAXReader();
// Document doc=read.read(new ByteArrayInputStream(sb.toString().getBytes()));
List list=doc.selectNodes("//class");
System.out.println("某学校共有"+list.size()+"个班级");
for(Iterator it=list.iterator();it.hasNext();){
Element eleClass=(Element)it.next();
String id=eleClass.attributeValue("name");
System.out.println("班级 "+id);
System.out.println("***********************");
for(Iterator it1=eleClass.elementIterator();it1.hasNext();){
Element eleStu=(Element)it1.next();
String name=eleStu.element("name").getText();
String age=eleStu.elementText("age");
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
实例三:(手动创建document并读取)
Document doc=DocumentHelper.createDocument();
Element eleResult=doc.addElement("result");
Element eleClas=eleResult.addElement("class");
eleClas.addAttribute("name", "1");
Element elestu=eleClas.addElement("student");
elestu.addAttribute("id", "1");
Element eleName=elestu.addElement("name");
eleName.addText("龙准");
Element eleAge=elestu.addElement("age");
eleAge.addText("25");
Element elestu2=eleClas.addElement("student").addAttribute("id", "2");
elestu2.addElement("name").setText("龙准");
elestu2.addElement("age").setText("23");
List list=doc.selectNodes("//class");
System.out.println("某学校共有"+list.size()+"个班级");
for(Iterator it=list.iterator();it.hasNext();){
Element eleClass=(Element)it.next();
String id=eleClass.attributeValue("name");
System.out.println("班级 "+id);
System.out.println("***********************");
for(Iterator it1=eleClass.elementIterator();it1.hasNext();){
Element eleStu=(Element)it1.next();
String name=eleStu.element("name").getText();
String age=eleStu.elementText("age");
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
}