我有以下 XML :
<soapenv:Envelope xmlns:soapenv="http://...." xmlns:web="http://...">
<soapenv:Body>
<web:ComponentExecutorExecuteRequest>
<web:group></web:group>
<web:payload><![CDATA[<SSC>
<SContext>
<BusinessUnit>E01</BusinessUnit>
</SContext>
<Payload>
<Filter>
<Expr operator="AND">
<Item name="/Ledger/Line/AccountingPeriod" operator="EQU" value="0122023" />
<Item name="/Ledger/Line/AccountCode" operator="EQU" value="262222150" />
<Item name="/Ledger/Line/TransactionDate" operator="BETWEEN" minvalue="23012024" maxvalue="23012024" />
</Expr>
</Filter>
<Select>
<Ledger>
<Line>
<AccountCode></AccountCode>
<AccountingPeriod></AccountingPeriod>
</Line>
</Ledger>
</Select>
</Payload>
</SSC>]]>
</web:payload>
</web:ComponentExecutorExecuteRequest>
</soapenv:Body>
</soapenv:Envelope>
私有字符串generarPayload(字符串accountingPeriod,字符串accountCode, 字符串 transactionDateInicio、字符串 transactionDateFin、字符串 token) 抛出异常{
Document doc = null;
doc = utiles.loadXMLFromResource("/com/..Siniestro.xml");
NodeList items = doc.getElementsByTagName("Item");
for (int j = 0; j < items.getLength(); j++) {
Node tmp = items.item(j);
if (tmp.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap atributos = tmp.getAttributes();
if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/AccountingPeriod")) {
if (accountingPeriod.equals("")) {
tmp.getParentNode().removeChild(tmp);
} else {
atributos.getNamedItem("value").setNodeValue(accountingPeriod);
}
}
if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/AccountCode")) {
if (accountCode.equals("")) {
tmp.getParentNode().removeChild(tmp);
} else {
atributos.getNamedItem("value").setNodeValue(accountCode);
}
}
if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/TransactionDate")) {
if (transactionDateInicio.equals("") || transactionDateFin.equals("")) {
tmp.getParentNode().removeChild(tmp);
} else {
atributos.getNamedItem("minvalue").setNodeValue(transactionDateInicio);
atributos.getNamedItem("maxvalue").setNodeValue(transactionDateFin);
}
}
}
}
这应该有帮助。它打印 CDATA 内的 XML。你必须单独解析它 -
Document doc = null;
doc = loadXMLFromResource("soap.xml");
String cdataValue = doc.getDocumentElement()
.getElementsByTagName("soapenv:Body").item(0)
.getChildNodes().item(1) // web:ComponentExecutorExecuteRequest
.getChildNodes().item(3) //[web:payload: null]
.getChildNodes().item(0).getNodeValue();
System.out.println(cdataValue);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc1 = docBuilder.parse(new InputSource(new StringReader(cdataValue)));
System.out.println(doc1.getDocumentElement().getChildNodes().item(1));