读取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