2

我从客户端收到 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方法(低效)

4

2 回答 2

2

你的 XPath 是错误的。您正在向 XPath 询问元素的内容,该内容确实是空的。为了获取属性,您需要单独获取它们:

/Esp:Interface/Esp:Transaction/@ActionCode

或者你可以一次得到它们

/Esp:Interface/Esp:Transaction/@*

但这会给您文档中的一组节点,每个节点都有一个名称和一个值,您必须在代码中提取它们。我不知道只获取文档中出现的属性字符串。

于 2013-08-26T13:19:43.803 回答
1

听起来您在<?xml ...?>. 确保文件正确保存为 UTF-8 并且声明之前没有空格。

为了解析属性,在这个简单的例子中,我推荐一个 SAX 解析器:

public class MySaxHandler extends DefaultHandler {
    public static void main(String[] args) throws Throwable {
        String sXMLData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
                + "<Esp:Interface Version=\"1.0\" xmlns:Esp=\"http://***Confidential****/\">\r\n"
                + "<Esp:Transaction ActionCode=\"APPROVE\" CardNumber=\"474849********28\" ExpiryDate=\"0824\" \r\n"
                + "    ResponseCode=\"00\" TerminalId=\"03563001\" Track2=\"474849********28=08241261234\" \r\n"
                + "    TransactionAmount=\"300\" TransactionId=\"00051\" Type=\"PURCHASE\" LocalDate=\"0823\" \r\n"
                + "    LocalTime=\"152006\" PosOperatorId=\"1234\" AuthorizationNumber=\"123456\" MessageType=\"REFERAL\">\r\n"
                + "</Esp:Transaction>\r\n" + "</Esp:Interface>";
        InputSource source = new InputSource(new StringReader(sXMLData));
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(new MySaxHandler());
        reader.parse(source);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
              Attributes atts) throws SAXException {
        if (qName.equals("Esp:Transaction")) {
            for (int i = 0; i < atts.getLength(); i++) {
                System.out.println(atts.getQName(i) + "=\"" + atts.getValue(i) + "\"");
            }
        }
    }
}

给出这个输出:

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"
于 2013-08-26T12:52:50.370 回答