讀取xml文件生成document對象
document轉換成string類型串
string串轉成xml
已知xml節點取節點值
已知xml節點修改節點值
一個xml文件:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <transaction> <body> <request> <trantyp>批量業務現存</trantyp> <acctnm> 0085213560 </acctnm> <acctno> 6225885517843413 </acctno> <avlbal> 201958.65 </avlbal> <accttyp> 0 </accttyp> <trantime> 20170801101030 </trantime> <currencytyp>cny</currencytyp> <trandesc></trandesc> <bal> 201958.65 </bal> <tranamt> 100000.00 </tranamt> </request> </body> <header> <msg> <sndtm> 101019 </sndtm> <msgcd>wcs0000200</msgcd> <seqnb> 632376531000009 </seqnb> <sndmbrcd> 5200 </sndmbrcd> <rcvmbrcd> 0000 </rcvmbrcd> <snddt> 20170821 </snddt> <sndappcd>cbs</sndappcd> <rcvappcd>wcs</rcvappcd> <calltyp>syn</calltyp> </msg> <ver> 1.0 </ver> <pnt> <sndtm> 101216 </sndtm> <sndmbrcd> 0000 </sndmbrcd> <rcvmbrcd> 0000 </rcvmbrcd> <snddt> 20170809 </snddt> <sndappcd>esb</sndappcd> <rcvappcd>wcs</rcvappcd> </pnt> </header> </transaction> |
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
|
package com.adtec.mq.client; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.file; import java.io.inputstream; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.transform.transformer; import javax.xml.transform.transformerfactory; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpressionexception; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document; import org.w3c.dom.node; public class test { /** * * @param document * document對象(讀xml生成的) * @return string字符串 * @throws throwable */ public string xmltostring(document document) throws throwable { transformerfactory ft = transformerfactory.newinstance(); transformer ff = ft.newtransformer(); ff.setoutputproperty( "encoding" , "gb2312" ); bytearrayoutputstream bos = new bytearrayoutputstream(); ff.transform( new domsource(document), new streamresult(bos)); return bos.tostring(); } /** * * @param xml形狀的str串 * @return document 對象 */ public document stringtoxml(string str) { stringbuilder sxml = new stringbuilder(); sxml.append(str); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); document doc = null ; try { inputstream is = new bytearrayinputstream(sxml.tostring().getbytes( "utf-8" )); doc = dbf.newdocumentbuilder().parse(is); is.close(); } catch (exception e) { e.printstacktrace(); } return doc; } /** * * @param document * @return 某個節點的值 前提是需要知道xml格式,知道需要取的節點相對根節點所在位置 */ public string getnodevalue(document document, string nodepaht) { xpathfactory xpfactory = xpathfactory.newinstance(); xpath path = xpfactory.newxpath(); string servinitrbrch = "" ; try { servinitrbrch = path.evaluate(nodepaht, document); } catch (xpathexpressionexception e) { e.printstacktrace(); } return servinitrbrch; } /** * * @param document * @param nodepath * 需要修改的節點相對根節點所在位置 * @param vodevalue * 替換的值 */ public void setnodevalue(document document, string nodepath, string vodevalue) { xpathfactory xpfactory = xpathfactory.newinstance(); xpath path = xpfactory.newxpath(); node node = null ; ; try { node = (node) path.evaluate(nodepath, document, xpathconstants.node); } catch (xpathexpressionexception e) { e.printstacktrace(); } node.settextcontent(vodevalue); } public static void main(string[] args) throws throwable { // 讀取xml文件,生成document對象 documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder(); // 文件的位置在工作空間的根目錄(位置隨意,只要寫對就ok) document document = builder.parse( new file( "a.xml" )); test t = new test(); // xml————》string string str = t.xmltostring(document); system.out.println( "str:" + str); // string ————》xml document doc = t.stringtoxml(str); string nodepath = "/transaction/header/msg/sndmbrcd" ; // getnodevalue string nodevalue = t.getnodevalue(doc, nodepath); system.out.println( "修改前nodevalue:" + nodevalue); // setnodevalue t.setnodevalue(doc, nodepath, nodevalue + "hello" ); system.out.println( "修改后nodevalue:" + t.getnodevalue(doc, nodepath)); } } |
測試結果:
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
|
str:<?xml version= "1.0" encoding= "utf-8" standalone= "no" ?><transaction> <body> <request> <trantyp>批量業務現存</trantyp> <acctnm> 0085213560 </acctnm> <acctno> 6225885517843413 </acctno> <avlbal> 201958.65 </avlbal> <accttyp> 0 </accttyp> <trantime> 20170801101030 </trantime> <currencytyp>cny</currencytyp> <trandesc/> <bal> 201958.65 </bal> <tranamt> 100000.00 </tranamt> </request> </body> <header> <msg> <sndtm> 101019 </sndtm> <msgcd>wcs0000200</msgcd> <seqnb> 632376531000009 </seqnb> <sndmbrcd> 5200 </sndmbrcd> <rcvmbrcd> 0000 </rcvmbrcd> <snddt> 20170821 </snddt> <sndappcd>cbs</sndappcd> <rcvappcd>wcs</rcvappcd> <calltyp>syn</calltyp> </msg> <ver> 1.0 </ver> <pnt> <sndtm> 101216 </sndtm> <sndmbrcd> 0000 </sndmbrcd> <rcvmbrcd> 0000 </rcvmbrcd> <snddt> 20170809 </snddt> <sndappcd>esb</sndappcd> <rcvappcd>wcs</rcvappcd> </pnt> </header> </transaction> 修改前nodevalue: 5200 修改后nodevalue:5200hello |
以上這篇string與xml互轉以及從xml取節點值并修改的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/leisure_life/article/details/78931577