-3

I am learning java. I am unable to parse the below XML using Java. Could someone explain me how to parse this xml?

Even i have tried with some sample code. That didn't work. I want to read all the present under transaction tag with

  • Transaction Id

  • amount

  • requestedAmount
  • status
  • Wallet Details
     <?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>

I would like to print all the details of under transaction tag.

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31
Hari
  • 89
  • 1
  • 2
  • 9
  • 3
    What did you try? https://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm#ADXDK3000 – duffymo Aug 13 '19 at 09:35
  • 1
    Possible duplicate of [Which is the best library for XML parsing in java](https://stackoverflow.com/questions/5059224/which-is-the-best-library-for-xml-parsing-in-java) – Phill Alexakis Aug 13 '19 at 09:37
  • One way to do it is to open a `Stream` at the location of your `xml` file and write everything into a `StringBuilder` then you simply have to convert it to a `String` and then use `split()` function to get the desired tag – Phill Alexakis Aug 13 '19 at 09:38
  • @PhillAlexakis, Could you please show me how? – Hari Aug 13 '19 at 09:50
  • @Hari sure i will show you a demo, keep in mind string parsing is a quite long process – Phill Alexakis Aug 13 '19 at 12:15
  • @Hari i suggest you do it with `xml` parser class and not in the way i showed you, but it's up to you – Phill Alexakis Aug 13 '19 at 12:27

1 Answers1

-1

Use the follow pattern in order to read a xml file with two transactions

  • Retrieving the WalletType Information
 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);

Please keep in mind that this will work just for two transactions

As you can see that's how i got the walletType attribute from your xml, based on what the split() method returns you can continue "cutting" the String to smaller parts until you get what you would like to have.

  • String data[]=content.split("<ns3:walletType>|</ns3:walletType>"); would also work but you would get the same wallet everytime.

That is why you need to have two separate Strings containing each transaction.

Note: This is the hard way, you can always do it using and xml parser class pick one from this post

If you get an exception at the xml parsing i suggest you insist on solving it, then everything would be much easier. Hope it helps

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31
  • @Hari not a problem, it would be highly appreciated if you marked it as an answer – Phill Alexakis Aug 13 '19 at 12:35
  • @NathanHughes it's the "hardcoded" way , that's why i suggested he uses a `parser` class, also the question says that he can't use a parser class, i don't know why – Phill Alexakis Aug 13 '19 at 13:51
  • Using regular expressions to parse XML *will* eventually break. See https://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg – VGR Aug 13 '19 at 14:21
  • yes my bad. but if the `xml` is the same it's gonna work, i don't disagree with you – Phill Alexakis Aug 14 '19 at 07:43