这篇文章主要介绍了Java根据XPATH批量替换XML节点中的值,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
测试环境:
JDK 1.8.0_25
代码实操:
message.xml文件
<Request service="SALE_ORDER_SERVICE" lang="zh-CN">
<Head>
<AccessCode>xxxxxxxxxx12Ur8v6qkYAaOf</AccessCode>
<Checkword>xxxxxxxxxxkmPif7Z35UEJjrR</Checkword>
</Head>
<Body>
<SaleOrderRequest>
<CompanyCode>Wxxxxxxxxxx</CompanyCode>
<SaleOrders>
<SaleOrder>
<WarehouseCode>xxxxxxx</WarehouseCode>
<ErpOrder>SO20220519xxxx13190</ErpOrder>
<ErpOrderType>3</ErpOrderType>
<orderType>10</orderType>
<receiverAddress>未知地址</receiverAddress>
<OrderReceiverInfo>
<ReceiverCompany>个人</ReceiverCompany>
<ReceiverName>凯琳</ReceiverName>
<ReceiverCountry>中国</ReceiverCountry>
<ReceiverProvince>广东省</ReceiverProvince>
<ReceiverCity>江门市</ReceiverCity>
<ReceiverArea>蓬江区</ReceiverArea>
<ReceiverAddress>详细地址</ReceiverAddress>
</OrderReceiverInfo>
<OrderItems>
<OrderItem>
<SkuNo>1001178</SkuNo>
<ItemName>保妇康栓1.74g*14粒/盒</ItemName>
<ItemQuantity>1</ItemQuantity>
</OrderItem>
<OrderItem>
<SkuNo>1008766</SkuNo>
<ItemName>美团小药箱</ItemName>
<ItemQuantity>1</ItemQuantity>
</OrderItem>
</OrderItems>
</SaleOrder>
</SaleOrders>
</SaleOrderRequest>
</Body>
</Request>
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<String> 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();
}
}
}
到此这篇关于Java 根据XPATH批量替换XML节点中的值的文章就介绍到这了,更多相关Java替换XML节点值内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Java 根据XPATH批量替换XML节点中的值
基础教程推荐
猜你喜欢
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java文件管理操作的知识点整理 2023-05-19
- Java实现线程插队的示例代码 2022-09-03
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java并发编程进阶之线程控制篇 2023-03-07
- springboot自定义starter方法及注解实例 2023-03-31
- Java数据结构之对象比较详解 2023-03-07
- java实现多人聊天系统 2023-05-19
- java基础知识之FileInputStream流的使用 2023-08-11
- Java实现查找文件和替换文件内容 2023-04-06