String与XML互转以及从XML取节点值并修改的方法

网友投稿 342 2023-01-25


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实现实例:

package com.aAYAEABtdtec.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

*http:// @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:

批量业务现存

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

修改前nodeValue:5200

修改后nodeValue:5200hello


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:邮件怎么连接共享文件夹(文件夹链接怎么发邮件)
下一篇:驱动接口自动化测试(接口自动化数据驱动)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~