我从客户端收到 XML 字符串
<?xml version="1.0" encoding="UTF-8"?>
<Esp:Interface Version="1.0" xmlns:Esp="http://***Confidential****/">
<Esp:Transaction ActionCode="APPROVE" CardNumber="474849********28" ExpiryDate="0824"
ResponseCode="00" TerminalId="03563001" Track2="474849********28=08241261234"
TransactionAmount="300" TransactionId="00051" Type="PURCHASE" LocalDate="0823"
LocalTime="152006" PosOperatorId="1234" AuthorizationNumber="123456" MessageType="REFERAL">
</Esp:Transaction>
</Esp:Interface>
我想提取每个字符串
ActionCode="APPROVE"
CardNumber="474849********28"
ExpiryDate="0824"
ResponseCode="00"
TerminalId="03563001"
Track2="474849********28=08241261234"
TransactionAmount="300"
TransactionId="00051"
Type="PURCHASE"
LocalDate="0823"
LocalTime="152006"
PosOperatorId="1234"
AuthorizationNumber="123456"
MessageType="REFERAL"
我曾经XPath
读过 XML String 但它给了我错误
InputSource source = new InputSource(new StringReader(sXMLData));//sXMLData Contains XML String
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String msg = xpath.evaluate("/Esp:Interface/Esp:Transaction", document);
System.out.println("msg= " + msg);
错误 :
msg : /* Coming Blank */
我引用了这两个链接,但没有得到太多帮助Parse XML Simple String using Java XPath And Simplest way to query XML in Java
我如何从 XML 中获取这些单独的字符串...通过使用内置XML
或旧substring
方法(低效)