String与XML互转以及从XML取节点值并修改的方法
程序员文章站
2024-02-13 15:17:52
读取xml文件生成document对象
document转换成string类型串
string串转成xml
已知xml节点取节点值
已知xml节点修改节点值
一个...
读取xml文件生成document对象
document转换成string类型串
string串转成xml
已知xml节点取节点值
已知xml节点修改节点值
一个xml文件:
<?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实现实例:
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)); } }
测试结果:
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取节点值并修改的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇: ASP.NET实现的简单易用文件上传类