尝试解析 xml 以从此 XML 获取“CreDtTm”标记值(未粘贴全部):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>SANDISS_2020_10_08_001</MsgId>
<CreDtTm>2020-10-15T18:15:33</CreDtTm>
<NbOfTxs>3</NbOfTxs>
<CtrlSum>36.00</CtrlSum>
<InitgPty>
<Nm>Bank</Nm>
<Id>
<OrgId>
<Othr>
<Id>40100</Id>
<SchmeNm>
<Cd>COID</Cd>
</SchmeNm>
</Othr>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
但是,并非所有值
这里是解析和编辑xml的方法(回写到xml文件跳过atm)
public void modifyXmlFile(String filePath, Map<String, String> tagValuesToChange) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.parse(filePath);
XPath xpath = XPathFactory.newInstance().newXPath();
for (Map.Entry<String, String> entry : tagValuesToChange.entrySet()) {
Node node = (Node) xpath.compile(entry.getKey()).evaluate(document, XPathConstants.NODE); //**This becomes null**
node.setTextContent(entry.getValue());
}
基本上没有找到标签并且节点变量设置为空。不知道为什么?你能帮我吗?