gpt4 book ai didi

java - 从 xml java 中提取字符串

转载 作者:行者123 更新时间:2023-12-04 04:44:14 24 4
gpt4 key购买 nike

我从客户端收到 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 字符串,但它给了我错误
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 方法(效率低下)

最佳答案

这听起来像是在 <?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"

关于java - 从 xml java 中提取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18444485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com