使用dom输出一个xml文件中的内容


1
import java.io.File; 2 import java.io.IOException; 3 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 8 import org.w3c.dom.Document; 9 import org.w3c.dom.NodeList; 10 import org.xml.sax.SAXException; 11 12 public class dom{ 13 public static void main(String args[]){ 14 15 16 //取得DocumentBuilderFactory类的对象 17 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 18 //取得DocumentBuilder类的对象 19 try { 20 DocumentBuilder builder = factory.newDocumentBuilder(); 21 22 Document doc =builder.parse(new File("src//dom.xml")); 23 NodeList list = doc.getElementsByTagName("name"); 24 //输出节点 25 System.out.println("姓名 : " + list.item(0).getFirstChild().getNodeValue()); 26 27 } catch (ParserConfigurationException e) { 28 e.printStackTrace(); 29 } catch (SAXException e) { 30 e.printStackTrace(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 35 36 37 38 39 } 40 }

此为上述使用的xml

1 <?xml version="1.0" encoding="GBK"?>
2 <addresslist>
3     <name>张浩威</name>
4 </addresslist>

 

1 输出结果
2 
3 
4 
5 
6 姓名 : 张浩威

 

 

 

较为复杂的xml文件可以用循环访问

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 
 5 import javax.xml.parsers.DocumentBuilder;
 6 import javax.xml.parsers.DocumentBuilderFactory;
 7 import javax.xml.parsers.ParserConfigurationException;
 8 
 9 import org.w3c.dom.Document;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.NodeList;
12 import org.xml.sax.SAXException;
13 
14 public class dom2{
15     public static void main(String args[]){
16         
17         
18         //取得DocumentBuilderFactory类的对象
19         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
20         //取得DocumentBuilder类的对象
21         try {
22             DocumentBuilder builder = factory.newDocumentBuilder();
23             
24             Document doc =builder.parse(new File("src//dom1.xml"));
25             
26             //得到所有linkman的节点
27             NodeList list = doc.getElementsByTagName("listman");
28             //输出节点
29             for(int i = 0 ; i < list.getLength() ; i++ ){
30             
31                 Element e = (Element)list.item(i);
32                 System.out.println("姓名 : " + e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
33                 System.out.println("邮箱 : " + e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
34             }
35         } catch (ParserConfigurationException e) {
36             e.printStackTrace();
37         } catch (SAXException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         }
42         
43         
44         
45         
46         
47     }
48 }
 1 <?xml version="1.0" encoding="GBK"?>
 2 <addresslist>
 3     <listman>
 4     <name>张浩威</name>
 5     <email>287877150@qq.com</email>
 6     </listman>
 7     <listman>
 8     <name>张浩龙</name>
 9     <email>94397959@qq.com</email>
10     </listman>
11 </addresslist>
1 输出结果:
2 
3 
4 
5 姓名 : 张浩威
6 邮箱 : 287877150@qq.com
7 姓名 : 张浩龙
8 邮箱 : 94397959@qq.com

 

 

也可以用dom来生成xml文件

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 
 5 import javax.xml.parsers.DocumentBuilder;
 6 import javax.xml.parsers.DocumentBuilderFactory;
 7 import javax.xml.parsers.ParserConfigurationException;
 8 import javax.xml.transform.OutputKeys;
 9 import javax.xml.transform.Transformer;
10 import javax.xml.transform.TransformerConfigurationException;
11 import javax.xml.transform.TransformerException;
12 import javax.xml.transform.TransformerFactory;
13 import javax.xml.transform.dom.DOMSource;
14 import javax.xml.transform.stream.StreamResult;
15 
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19 import org.xml.sax.SAXException;
20 
21 
22 //Dom操作除了可以进行解析之外还可以进行文件的生成
23 public class dom3{
24     public static void main(String args[]){
25         
26         
27         //取得DocumentBuilderFactory类的对象
28         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
29         //取得DocumentBuilder类的对象
30         try {
31             DocumentBuilder builder = factory.newDocumentBuilder();
32             
33             //创建一个新的xml文档
34             Document doc =builder.newDocument();
35             Element addresslist = doc.createElement("addresslist");
36             Element listman = doc.createElement("listman");
37             Element name = doc.createElement("name");
38             Element email = doc.createElement("email");
39             
40             //设置节点内容
41             name.appendChild(doc.createTextNode("张浩威"));
42             email.appendChild(doc.createTextNode("287877150@qq.com"));
43             //设置各节点的关系
44             //将节点name 和 email设置为listman的子节点
45             listman.appendChild(name);
46             listman.appendChild(email);
47             //将listman设置为addresslist的子节点
48             addresslist.appendChild(listman);
49             //设置根节点
50             doc.appendChild(addresslist);
51 
52             //得到所有linkman的节点
53             NodeList list = doc.getElementsByTagName("listman");
54             //这段代码的关键是将文档内容保存在文件中
55             TransformerFactory tf = TransformerFactory.newInstance();
56             Transformer transformer = tf.newTransformer();
57             //设置输出编码
58             transformer.setOutputProperty(OutputKeys.ENCODING, "GBK");
59             //准备输出文档
60             DOMSource source = new DOMSource(doc);
61             StreamResult result = new StreamResult(new File("dom2.xml"));
62             transformer.transform(source, result);
63             
64             
65         } catch (ParserConfigurationException e) {
66             e.printStackTrace();
67         } catch (TransformerConfigurationException e) {
68             e.printStackTrace();
69         } catch (TransformerException e) {
70             e.printStackTrace();
71         } 
72         
73         
74         //此时可以完成文档的输出操作,但是输出后的xml文档格式非常不好
75         //对于dom操作既可以读取也可以输出和修改,更多的操作在于读取
76         
77         
78     }
79 }
1 输出结果
2 格式很难看,但是符合标准
3 <?xml version="1.0" encoding="GBK" standalone="no"?><addresslist><listman><name>张浩威</name><email>287877150@qq.com</email></listman></addresslist>
posted on 2012-09-08 15:59  elleniou  阅读(1248)  评论(0编辑  收藏  举报