激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 利用dom方式讀取、創建xml詳解及實例代碼

Java 利用dom方式讀取、創建xml詳解及實例代碼

2020-08-28 11:01Java之家 Java教程

這篇文章主要介紹了Java 利用dom方式讀取、創建xml的相關資料,需要的朋友可以參考下

Java 利用dom方式讀取、創建xml詳解

1.創建一個接口

XmlInterface.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface XmlInterface
 {
 
  /**
  *
 建立XML文檔
  *
 @param fileName 文件全路徑名稱
  */
  public void createXml(String
 fileName);
  /**
  *
 解析XML文檔
  *
 @param fileName 文件全路徑名稱
  */
  public void parserXml(String
 fileName);
}

接口實現

XmlImpl.java

?
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.test.xml;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
public class XmlImpl implements XmlInterface{
 private Document
 document;
 
 public void init()
 {
  try {
   DocumentBuilderFactory
 factory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder
 builder = factory.newDocumentBuilder();
   this.document
 = builder.newDocument();
  } catch (ParserConfigurationException
 e) {
   System.out.println(e.getMessage());
  }
 }
 
 public void createXml(String
 fileName) {
  Element
 root = this.document.createElement("scores");
  this.document.appendChild(root);
  Element
 employee = this.document.createElement("employee");
  Element
 name = this.document.createElement("name");
  name.appendChild(this.document.createTextNode("wangchenyang"));
  employee.appendChild(name);
  Element
 sex = this.document.createElement("sex");
  sex.appendChild(this.document.createTextNode("m"));
  employee.appendChild(sex);
  Element
 age = this.document.createElement("age");
  age.appendChild(this.document.createTextNode("26"));
  employee.appendChild(age);
  root.appendChild(employee);
  TransformerFactory
 tf = TransformerFactory.newInstance();
  try {
   Transformer
 transformer = tf.newTransformer();
   DOMSource
 source = new DOMSource(document);
   transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
   PrintWriter
 pw = new PrintWriter(new FileOutputStream(fileName));
   StreamResult
 result = new StreamResult(pw);
   transformer.transform(source,
 result);
   System.out.println("生成XML文件成功!");
  } catch (TransformerConfigurationException
 e) {
   System.out.println(e.getMessage());
  } catch (IllegalArgumentException
 e) {
   System.out.println(e.getMessage());
  } catch (FileNotFoundException
 e) {
   System.out.println(e.getMessage());
  } catch (TransformerException
 e) {
   System.out.println(e.getMessage());
  }
 }
 
 public void parserXml(String
 fileName) {
  try {
   DocumentBuilderFactory
 dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder
 db = dbf.newDocumentBuilder();
   Document
 document = db.parse(fileName);
    
   NodeList
 employees = document.getChildNodes();
   for (int i
 = 0;
 i < employees.getLength(); i++) {
    Node
 employee = employees.item(i);
    NodeList
 employeeInfo = employee.getChildNodes();
    for (int j
 = 0;
 j < employeeInfo.getLength(); j++) {
     Node
 node = employeeInfo.item(j);
     NodeList
 employeeMeta = node.getChildNodes();
     for (int k
 = 0;
 k < employeeMeta.getLength(); k++) {
      System.out.println(employeeMeta.item(k).getNodeName()
        + ":" +
 employeeMeta.item(k).getTextContent());
     }
    }
   }
   System.out.println("解析完畢");
  } catch (FileNotFoundException
 e) {
   System.out.println(e.getMessage());
  } catch (ParserConfigurationException
 e) {
   System.out.println(e.getMessage());
  } catch (SAXException
 e) {
   System.out.println(e.getMessage());
  } catch (IOException
 e) {
   System.out.println(e.getMessage());
  }
 }
}

測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main
 {
 
 public static void main(String
 args[]){
  XmlImpl
 dd=new XmlImpl();
  String
 str="D:/grade.xml";
  dd.init();
  dd.createXml(str); //創建xml
  dd.parserXml(str); //讀取xml
 }
}

結果

生成xml

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="GB2312"?>
<scores>
<employee>
<name>wangchenyang</name>
<sex>m</sex>
<age>26</age>
</employee>
</scores>

讀取xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
生成XML文件成功!
#text:
 
name:wangchenyang
#text:
 
sex:m
#text:
 
age:26
#text:
 
解析完畢

感謝閱讀,希望能幫到大家,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2021免费日韩视频网 | 欧美一级片一区 | 久久免费视频7 | 日韩视频―中文字幕 | 亚洲成人久久精品 | 鲁丝一区二区三区不属 | 在线观看国产日韩 | 91久久线看在观草草青青 | 毛片在线视频免费观看 | 国产午夜亚洲精品理论片大丰影院 | 毛片免费试看 | 国产日本在线 | 亚洲一区二区三区日本久久九 | 色屁屁xxxxⅹ免费视频 | 成人免费毛片在线观看 | 免费嗨片首页中文字幕 | 亚洲成人精品区 | 性高跟鞋xxxxhd4kvideos | 成年免费在线视频 | 欧美一级高潮 | 久久久久久久久久久久久久久久久久 | 羞羞视频免费网站男男 | 亚洲国产精品久久久久久久久久 | 精品国产一区二区在线观看 | 国产91一区二区三区 | 日本在线播放一区二区 | 87成人免费看片 | 国产一区日韩一区 | 黄色片免费在线 | 成人羞羞视频在线观看免费 | 久久久婷婷一区二区三区不卡 | 特一级黄色毛片 | 一级做a爱片性色毛片 | 国产精品午夜未成人免费观看 | 久久免费视频精品 | 久久99精品久久久久久小说 | 爱射av | 在线a亚洲视频播放在线观看 | 国产精品午夜小视频观看 | 欧美顶级毛片在线播放小说 | 日韩精品中文字幕一区二区 |