根据XPATH批量替换XML节点中的值
by: 授客 QQ:1033553122
测试环境
JDK 1.8.0_25
代码实操
_message.xml_文件
xxxxxxxxxx12Ur8v6qkYAaOf
xxxxxxxxxxkmPif7Z35UEJjrR
Wxxxxxxxxxx
xxxxxxx
SO20220519xxxx13190
3
10
未知地址
个人
凯琳
中国
广东省
江门市
蓬江区
详细地址
1001178
保妇康栓1.74g*14粒/盒
1
1008766
美团小药箱
1
XMLHelper.java
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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 java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class XMLHelper {
// XML文件转XML文档对象
public static Document xmlFileToDocument(String filePath) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(new File(filePath)));
return doc;
}
// XML文档转XML字符串
public static String docToString(Document doc) throws TransformerException {
TransformerFactory ft = TransformerFactory.newInstance();
Transformer ff = ft.newTransformer();
ff.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
ff.setOutputProperty("encoding", "gbk");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ff.transform(new DOMSource(doc), new StreamResult(bos));
return bos.toString();
}
// XML字符串转XML文档对象
public static Document stringToDocument(String xmlData) throws Exception {
StringReader sr = new StringReader(xmlData);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
return doc;
}
// 替换单个XPATH指定的XML节点值
public static void replaceXmlNodeValue(Document doc, String nodePath, String value) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
// 匹配需替换的节点
NodeList nodeList = (NodeList) xpath.evaluate(nodePath, doc, XPathConstants.NODESET);
// 单个XPATH可能匹配多个XML节点,所以需要遍历替换
for (int i = 0; i < nodeList.getLength(); i++) {
nodeList.item(i).setTextContent(value);
}
}
// 批量多个XPATH指定的XML节点值
public static void replaceXmlNodesValue(Document doc, List nodesPath, String value) throws XPathExpressionException {
for (String nodePath : nodesPath) {
XMLHelper.replaceXmlNodeValue(doc, nodePath, value);
}
}
public static void replaceReceiverAddress(Document doc, String value) throws Exception{
List nodesPath= new ArrayList();
nodesPath.add(".//OrderReceiverInfo/ReceiverAddress");
nodesPath.add(".//receiverAddress");
XMLHelper.replaceXmlNodesValue(doc, nodesPath, value);
}
public static void main(String []args) {
try {
Document doc = XMLHelper.xmlFileToDocument("message.xml");
// 替换地址
XMLHelper.replaceReceiverAddress(doc, "吉祥街道幸福社区1号大楼23号");
String newXml = XMLHelper.docToString(doc);
System.out.println(newXml);
} catch (Exception e) {
e.printStackTrace();
}
}
}
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
文字过少占位符,可无视
Original: https://www.cnblogs.com/shouke/p/16663766.html
Author: 授客
Title: Java 根据XPATH批量替换XML节点中的值
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620306/
转载文章受原作者版权保护。转载请注明原作者出处!