读取xml文件生成document对象
document转换成String类型串
String串转成xml
已知xml节点取节点值
已知xml节点修改节点值
一个xml文件:
批量业务现存 0085213560 6225885517843413 201958.65 020170801101030 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.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 {
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();
}
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;
}
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;
}
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:修改前nodevalue:5200 修改后nodevalue:5200hello 批量业务现存 0085213560 6225885517843413 201958.65 020170801101030 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取节点值并修改的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



