从xml java中提取字符串

时间:2013-08-26 12:46:43

标签: java xml xml-parsing

我从客户端收到XML String

<?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 XPathSimplest way to query XML in Java

如何从XML中获取这些单独的字符串...使用XML或旧substring方法构建(效率低下)

2 个答案:

答案 0 :(得分:2)

你的XPath错了。你问XPath元素的内容,这确实是空的。为了获得属性,您需要单独获取它们:

/Esp:Interface/Esp:Transaction/@ActionCode

或者你可以一次性完成所有这些

/Esp:Interface/Esp:Transaction/@*

但是这将从文档中为您提供一组节点,每个节点都有一个名称和一个值,您必须在代码中提取这些节点。我不知道如何获得文档中出现的属性字符串。

答案 1 :(得分: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"