怎样用java代码将xml文件内容写入txt文件中
1个回答
展开全部
我以前学dom解析的时候写了一个小例子,你参考参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.lhx.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Test {
public static void main(String[] args) {
DocumentBuilderFactory fct=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder bui=fct.newDocumentBuilder();
Document doc=bui.newDocument();
Element ps=doc.createElement("persons");
Element p1=doc.createElement("person");
Element p2=doc.createElement("person");
Attr id1=doc.createAttribute("id");
Attr id2=doc.createAttribute("id");
id1.setNodeValue("1");
id2.setNodeValue("2");
Element name1=doc.createElement("name");
Text na1=doc.createTextNode("龙大哥");
Element name2=doc.createElement("name");
Text na2=doc.createTextNode("龙大爷");
Element sex1=doc.createElement("sex");
Text se1=doc.createTextNode("帅哥");
Element sex2=doc.createElement("sex");
Text se2=doc.createTextNode("妹子");
doc.appendChild(ps);
ps.appendChild(p1);
p1.appendChild(name1);
p1.setAttributeNode(id1);
name1.appendChild(na1);
p1.appendChild(sex1);
sex1.appendChild(se1);
ps.appendChild(p2);
p2.appendChild(name2);
p2.setAttributeNode(id2);
name2.appendChild(na2);
p2.appendChild(sex2);
sex2.appendChild(se2);
try {
FileOutputStream fos=new FileOutputStream(new File("E:/longdada.xml"));
try {
((org.apache.crimson.tree.XmlDocument)doc)
.write(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.lhx.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Test {
public static void main(String[] args) {
DocumentBuilderFactory fct=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder bui=fct.newDocumentBuilder();
Document doc=bui.newDocument();
Element ps=doc.createElement("persons");
Element p1=doc.createElement("person");
Element p2=doc.createElement("person");
Attr id1=doc.createAttribute("id");
Attr id2=doc.createAttribute("id");
id1.setNodeValue("1");
id2.setNodeValue("2");
Element name1=doc.createElement("name");
Text na1=doc.createTextNode("龙大哥");
Element name2=doc.createElement("name");
Text na2=doc.createTextNode("龙大爷");
Element sex1=doc.createElement("sex");
Text se1=doc.createTextNode("帅哥");
Element sex2=doc.createElement("sex");
Text se2=doc.createTextNode("妹子");
doc.appendChild(ps);
ps.appendChild(p1);
p1.appendChild(name1);
p1.setAttributeNode(id1);
name1.appendChild(na1);
p1.appendChild(sex1);
sex1.appendChild(se1);
ps.appendChild(p2);
p2.appendChild(name2);
p2.setAttributeNode(id2);
name2.appendChild(na2);
p2.appendChild(sex2);
sex2.appendChild(se2);
try {
FileOutputStream fos=new FileOutputStream(new File("E:/longdada.xml"));
try {
((org.apache.crimson.tree.XmlDocument)doc)
.write(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询