Skip to content

Commit 528b759

Browse files
xml和String的转换
1 parent 35801a4 commit 528b759

File tree

1 file changed

+81
-37
lines changed

1 file changed

+81
-37
lines changed

src/main/java/com/XmlUtils.java

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,96 @@
11
package com;
22

3-
import org.dom4j.Attribute;
4-
import org.dom4j.Document;
5-
import org.dom4j.Element;
3+
import org.dom4j.*;
4+
import org.dom4j.io.OutputFormat;
65
import org.dom4j.io.SAXReader;
6+
import org.dom4j.io.XMLWriter;
77

8-
import java.io.File;
8+
import java.io.*;
99

1010
/**
1111
* Created by lenovo on 二月
1212
*/
1313
public class XmlUtils {
14-
public void getXmlContent() throws Exception {
15-
SAXReader saxReader=new SAXReader();
16-
//获取文件,转换成Document对象
17-
Document document=saxReader.read( new File("E:\\test.xml"));
18-
//获取根元素
19-
Element root=document.getRootElement();
20-
//文档转换为字符串
21-
String docXmlText=document.asXML();
22-
System.out.println("xml内容如下:");
23-
System.out.println(docXmlText);
24-
System.out.println("根目录标签的内容如下:");
25-
String rootXmlText=root.asXML();
26-
System.out.println(rootXmlText);
27-
System.out.println("子标签<西游记>的内容如下:");
28-
Element e1=root.element("西游记");
29-
System.out.println(e1.asXML());
30-
System.out.println("子标签<作者>的内容如下:");
31-
Element e2=e1.element("作者");
32-
System.out.println(e2.asXML());
33-
System.out.println("标签内容如下:");
34-
String text=e2.getText();
35-
// String text= e2.asXML().substring(e2.asXML().indexOf(">") + 1, e2.asXML().lastIndexOf("<"));
36-
System.out.println(text);
37-
}
14+
/**
15+
* 获取xml文件的内容
16+
* @param fileName 文件名,类似 String fileName1="E:\\test.xml";
17+
* @param names 标签组成的字符数组.比如 String[] names=new String[]{"西游记","作者"};
18+
* @throws Exception
19+
*/
20+
public void getXmlContent(String fileName,String[] names) throws Exception {
21+
SAXReader saxReader=new SAXReader();
22+
//获取文件,转换成Document对象
23+
Document document=saxReader.read( new File(fileName));
24+
//获取xml内容
25+
String docXmlText=document.asXML();
26+
System.out.println("xml内容如下:"+docXmlText);
27+
System.out.println("-----------------------------");
28+
//获取根元素
29+
Element e=document.getRootElement();
30+
//遍历xml文档
31+
for(String name : names) {
32+
if ( e!=null && e.element(name)!=null) {
33+
e=e.element(name);
34+
String elementStr=e.asXML();
35+
System.out.println(elementStr);
36+
System.out.println("-----------------------------");
37+
}
38+
}
39+
String text=e.getText();
40+
System.out.println("元素内容如下:\n"+text);
41+
}
3842

3943

40-
public void getElementText() throws Exception{
41-
SAXReader saxReader=new SAXReader();
42-
//获取文件,转换成Document对象
43-
Document document=saxReader.read( new File("E:\\test.xml"));
44-
Element root=document.getRootElement();
45-
Attribute attribute=root.attribute("四大名著");
46-
String text=attribute.getText();
47-
System.out.println(text);
44+
/**
45+
* 字符串写入xml文件
46+
* @param text 文本内容,类似 String text="<csdn><java>Java班</java><net>Net班</net></csdn>";
47+
* @throws Exception
48+
*/
49+
public void stringToXml(String text,String fileName) throws Exception {
50+
Document document= DocumentHelper.parseText(text);
51+
Element element=document.getRootElement();
52+
System.out.println(element.getName());
53+
writeToXml(document,fileName);
54+
}
4855

56+
/**
57+
* 把document对象写入xml文件中
58+
* @param document
59+
*/
60+
public void writeToXml(Document document,String fileName) throws Exception {
61+
// 紧凑的格式
62+
// OutputFormat format=OutputFormat.createCompactFormat();
63+
// 排版缩进的格式
64+
OutputFormat format=OutputFormat.createPrettyPrint();
65+
format.setEncoding("UTF-8");
66+
//创建XmlWriter对象,指定文件及编码
67+
XMLWriter xmlWriter=new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)),"UTF-8"),format);
68+
xmlWriter.write(document);
69+
//刷新流
70+
xmlWriter.flush();
71+
xmlWriter.close();
72+
}
4973

74+
public void createDocumentToXml(String fileName) throws Exception{
75+
Document document=DocumentHelper.createDocument();
76+
Element root=document.addElement("csdn");
77+
Element java=root.addElement("java");
78+
java.setText("java班");
79+
Element ios=root.addElement("ios");
80+
ios.setText("ios班");
81+
writeToXml(document,fileName);
82+
}
5083

51-
}
84+
// public void getElementText() throws Exception{
85+
// SAXReader saxReader=new SAXReader();
86+
// //获取文件,转换成Document对象
87+
// Document document=saxReader.read( new File("E:\\test.xml"));
88+
// Element root=document.getRootElement();
89+
// Attribute attribute=root.attribute("四大名著");
90+
// String text=attribute.getText();
91+
// System.out.println(text);
92+
//
93+
//
94+
//
95+
// }
5296
}

0 commit comments

Comments
 (0)