【问题标题】:How to parse the xml?如何解析xml?
【发布时间】:2019-12-19 20:16:11
【问题描述】:

我正在学习java。 我无法使用 Java 解析以下 XML。有人可以解释一下如何解析这个 xml 吗?

即使我尝试过一些示例代码。那没有用。 我想用

阅读交易标签下的所有礼物
  • 事务 ID

  • 金额

  • 请求的金额
  • 状态
  • 钱包详情
     <?xml version="1.0" encoding="UTF-8"?>
    <ns2:getFundAccountHistoryResponse xmlns:ns2="http://schema.products.sports.bet.com/fundingService" xmlns="http://schema.products.sports.bet.com/promoCommonTypes" xmlns:ns3="http://schema.products.sports.bet.com/fundingTypes">
       <ns2:status code="OK" />
       <ns2:transactions>
          <ns3:transaction id="74608009" amount="-200.00" requestedAmount="-200.00" status="COMPLETE">
             <ns3:wallets>
                <ns3:wallet>
                   <ns3:walletType>NON_WITHDRAWABLE</ns3:walletType>
                   <ns3:totalBalance>0.00</ns3:totalBalance>
                </ns3:wallet>
             </ns3:wallets>
             <ns3:transactionFunds>
                <ns3:transactionFund>
                   <ns3:externalFundRef id="CPM-1073070" provider="OB.CUST" />
                   <ns3:transactionFundItems>
                      <ns3:transactionFundItem forfeited="false">
                         <ns3:type>CASH</ns3:type>
                         <ns3:amount>-200.00</ns3:amount>
                      </ns3:transactionFundItem>
                   </ns3:transactionFundItems>
                </ns3:transactionFund>
             </ns3:transactionFunds>
             <ns3:transactionType>BSTK</ns3:transactionType>
             <ns3:creationDate>2019-08-10T17:44:31</ns3:creationDate>
             <ns3:publishedTransactionDate>2019-08-10T17:44:31</ns3:publishedTransactionDate>
             <ns3:transactionDate>2019-08-10T17:44:31</ns3:transactionDate>
             <ns3:fundingActivity>
                <ns3:externalActivityRef id="60077338_0" provider="OB.BET" />
                <ns3:type>STAKE</ns3:type>
                <ns3:fundingOperations>
                   <ns3:fundingOperation>
                      <ns3:externalOperationRef id="b703c6b9-b9aa-4c5d-b6cc-1321d7f2a7a9" provider="OB.BET" />
                      <ns3:operationType>ESB</ns3:operationType>
                      <ns3:status>OPEN</ns3:status>
                   </ns3:fundingOperation>
                </ns3:fundingOperations>
             </ns3:fundingActivity>
             <ns3:description>|Gold Coast Titans| @ 4.75</ns3:description>
          </ns3:transaction>
          <ns3:transaction id="74606629" amount="-411.00" requestedAmount="-411.00" status="COMPLETE">
             <ns3:wallets>
                <ns3:wallet>
                   <ns3:walletType>NON_WITHDRAWABLE</ns3:walletType>
                   <ns3:totalBalance>0.00</ns3:totalBalance>
                </ns3:wallet>
             </ns3:wallets>
             <ns3:transactionFunds>
                <ns3:transactionFund>
                   <ns3:externalFundRef id="CPM-1033856" provider="OB.CUST" />
                   <ns3:transactionFundItems>
                      <ns3:transactionFundItem forfeited="false">
                         <ns3:type>CASH</ns3:type>
                         <ns3:amount>-14.04</ns3:amount>
                      </ns3:transactionFundItem>
                   </ns3:transactionFundItems>
                </ns3:transactionFund>
                <ns3:transactionFund>
                   <ns3:externalFundRef id="CPM-1073070" provider="OB.CUST" />
                   <ns3:transactionFundItems>
                      <ns3:transactionFundItem forfeited="false">
                         <ns3:type>CASH</ns3:type>
                         <ns3:amount>-396.96</ns3:amount>
                      </ns3:transactionFundItem>
                   </ns3:transactionFundItems>
                </ns3:transactionFund>
             </ns3:transactionFunds>
             <ns3:transactionType>BSTK</ns3:transactionType>
             <ns3:creationDate>2019-08-10T17:42:08</ns3:creationDate>
             <ns3:publishedTransactionDate>2019-08-10T17:42:08</ns3:publishedTransactionDate>
             <ns3:transactionDate>2019-08-10T17:42:08</ns3:transactionDate>
             <ns3:fundingActivity>
                <ns3:externalActivityRef id="60076164_0" provider="OB.BET" />
                <ns3:type>STAKE</ns3:type>
                <ns3:fundingOperations>
                   <ns3:fundingOperation>
                      <ns3:externalOperationRef id="9700030e-8057-4ca4-8a1f-74ba9799980d" provider="OB.BET" />
                      <ns3:operationType>ESB</ns3:operationType>
                      <ns3:status>OPEN</ns3:status>
                   </ns3:fundingOperation>
                </ns3:fundingOperations>
             </ns3:fundingActivity>
             <ns3:description>|Over| (49.0) @ 1.87</ns3:description>
          </ns3:transaction>

       </ns2:transactions>
    </ns2:getFundAccountHistoryResponse>

我想打印交易标签下的所有详细信息。

【问题讨论】:

标签: java parsing xml-parsing


【解决方案1】:

使用以下模式来读取包含两个事务的xml 文件

  • 检索WalletType信息
 StringBuilder sb= new StringBuilder();
 String sampleLine;
 try(BufferedReader sampleBuffer = new BufferedReader(new FileReader("./test.xml"))) {
        while ((sampleLine = sampleBuffer.readLine()) != null) {
          sb.append(sampleLine);
            }
 } catch (Exception e) {
            e.printStackTrace();
  }
//System.out.println(sb.toString());

String content=sb.toString();
String data[]=content.split("<ns3:transaction id=\"74608009\" amount=\"-200.00\" requestedAmount=\"-200.00\" status=\"COMPLETE\">|</ns3:transaction>");
// System.out.println(data[1]+"\n\n");
String seconddata[]=data[1].split("<ns3:walletType>|</ns3:walletType>");
String walletType=seconddata[1];
System.out.println("Wallet Type: "+walletType);

请记住,这仅适用于两个交易

正如您所看到的,这就是我从您的xml 获得walletType 属性的方式,基于split() 方法返回的内容,您可以继续将String“切割”成更小的部分,直到您得到您想要的喜欢。

  • String data[]=content.split("&lt;ns3:walletType&gt;|&lt;/ns3:walletType&gt;"); 也可以,但你每次都会得到同一个钱包。

这就是为什么您需要让two 单独的Strings 包含每个事务。

注意:这是 hard 方法,您始终可以使用 xml 解析器类从 this post 中选择一个

如果您在xml 解析中遇到异常,我建议您坚持解决它,那么一切都会容易得多。希望对你有帮助

【讨论】:

  • @Hari 没问题,如果您将其标记为答案,我们将不胜感激
  • @NathanHughes 这是“硬编码”方式,这就是为什么我建议他使用parser 类,还有问题说他不能使用解析器类,我不知道为什么
  • 使用正则表达式解析XML最终崩溃。见stackoverflow.com/questions/701166/…
  • 是的,我的错。但如果xml 相同,它会起作用,我不同意你的观点
猜你喜欢
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2014-05-13
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多