String与XML互转以及从XML取节点值并修改的方法
读取xml文件生成Document对象
Document转换成String类型串
String串转成xml
已知xml节点取节点值
已知xml节点修改节点值
一个xml文件:
批量业务现存 0085213560 6225885517843413 201958.65 0 20170801101030 CNY 201958.65 100000.00 101019 WCS0000200 632376531000009 5200 0000 20170821 CBS WCS SYN 1.0 101216 0000 0000 20170809 ESB WCS
java实现实例:
packagecom.adtec.mq.client; importjava.io.ByteArrayInputStream; importjava.io.ByteArrayOutputStream; importjava.io.File; importjava.io.InputStream; importjavax.xml.parsers.DocumentBuilder; importjavax.xml.parsers.DocumentBuilderFactory; importjavax.xml.transform.Transformer; importjavax.xml.transform.TransformerFactory; importjavax.xml.transform.dom.DOMSource; importjavax.xml.transform.stream.StreamResult; importjavax.xml.xpath.XPath; importjavax.xml.xpath.XPathConstants; importjavax.xml.xpath.XPathExpressionException; importjavax.xml.xpath.XPathFactory; importorg.w3c.dom.Document; importorg.w3c.dom.Node; publicclassTest{ /** * *@paramdocument *Document对象(读xml生成的) *@returnString字符串 *@throwsThrowable */ publicStringxmlToString(Documentdocument)throwsThrowable{ TransformerFactoryft=TransformerFactory.newInstance(); Transformerff=ft.newTransformer(); ff.setOutputProperty("encoding","GB2312"); ByteArrayOutputStreambos=newByteArrayOutputStream(); ff.transform(newDOMSource(document),newStreamResult(bos)); returnbos.toString(); } /** * *@paramxml形状的str串 *@returnDocument对象 */ publicDocumentStringTOXml(Stringstr){ StringBuildersXML=newStringBuilder(); sXML.append(str); DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance(); Documentdoc=null; try{ InputStreamis=newByteArrayInputStream(sXML.toString().getBytes("utf-8")); doc=dbf.newDocumentBuilder().parse(is); is.close(); }catch(Exceptione){ e.printStackTrace(); } returndoc; } /** * *@paramdocument *@return某个节点的值前提是需要知道xml格式,知道需要取的节点相对根节点所在位置 */ publicStringgetNodeValue(Documentdocument,StringnodePaht){ XPathFactoryxpfactory=XPathFactory.newInstance(); XPathpath=xpfactory.newXPath(); StringservInitrBrch=""; try{ servInitrBrch=path.evaluate(nodePaht,document); }catch(XPathExpressionExceptione){ e.printStackTrace(); } returnservInitrBrch; } /** * *@paramdocument *@paramnodePath *需要修改的节点相对根节点所在位置 *@paramvodeValue *替换的值 */ publicvoidsetNodeValue(Documentdocument,StringnodePath,StringvodeValue){ XPathFactoryxpfactory=XPathFactory.newInstance(); XPathpath=xpfactory.newXPath(); Nodenode=null; ; try{ node=(Node)path.evaluate(nodePath,document,XPathConstants.NODE); }catch(XPathExpressionExceptione){ e.printStackTrace(); } node.setTextContent(vodeValue); } publicstaticvoidmain(String[]args)throwsThrowable{ //读取xml文件,生成document对象 DocumentBuilderbuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder(); //文件的位置在工作空间的根目录(位置随意,只要写对就ok) Documentdocument=builder.parse(newFile("a.xml")); Testt=newTest(); //XML————》String Stringstr=t.xmlToString(document); System.out.println("str:"+str); //String————》XML Documentdoc=t.StringTOXml(str); StringnodePath="/transaction/header/msg/sndMbrCd"; //getNodeValue StringnodeValue=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:修改前nodeValue:5200 修改后nodeValue:5200hello 批量业务现存 0085213560 6225885517843413 201958.65 0 20170801101030 CNY 201958.65 100000.00 101019 WCS0000200 632376531000009 5200 0000 20170821 CBS WCS SYN 1.0 101216 0000 0000 20170809 ESB WCS
以上这篇String与XML互转以及从XML取节点值并修改的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。