Parsing XML - Need Help Extracting Data

Discussion in 'C#' started by nyxano, Nov 18, 2009.

  1. #1
    Hello,

    Using Classic ASP (not ASP.net) and am trying to integrate with USPS Real Time Shipping and I am having trouble parsing and extracting data from the International XML Reponse.

    I have the code loaded into the XML Parser but I am just unsure of the actual code to extract exactly what I need.

    The response is similar to what is below.

    <IntlRateResponse>
      <Package ID="0">
        <Service ID="1">
          <Pounds>1</Pounds>
          <Ounces>14</Ounces>
          <MailType>Package</MailType>
          <Country>AUSTRALIA</Country>
          <Postage>37.75</Postage>
          <ValueOfContents>4,500.00</ValueOfContents>
          <Insurance>13.35</Insurance>
          <SvcCommitments>5 Days</SvcCommitments>
          <SvcDescription>Express Mail International (EMS)</SvcDescription>
        </Service>
        <Service ID="10">
          <Pounds>1</Pounds>
          <Ounces>14</Ounces>
          <MailType>Package</MailType>
          <Country>AUSTRALIA</Country>
          <Postage>27.95</Postage>
          <ValueOfContents>4,500.00</ValueOfContents>
          <Insurance>13.35</Insurance>
          <SvcCommitments>5 Days</SvcCommitments>
          <SvcDescription>Express Mail International (EMS) Flat-Rate Envelope</SvcDescription>
        </Service>
        <Service ID="2">
          <Pounds>1</Pounds>
          <Ounces>14</Ounces>
          <MailType>Package</MailType>
          <Country>AUSTRALIA</Country>
          <Postage>31.35</Postage>
          <ValueOfContents>4,500.00</ValueOfContents>
          <InsComment>INSURED VALUE</InsComment>
          <ParcelIndemnityCoverage>70.43</ParcelIndemnityCoverage>
          <SvcCommitments>6 - 10 Days</SvcCommitments>
          <SvcDescription>Priority Mail International</SvcDescription>
        </Service>
        <Service ID="8">
          <Pounds>1</Pounds>
          <Ounces>14</Ounces>
          <MailType>Package</MailType>
          <Country>AUSTRALIA</Country>
          <Postage>12.95</Postage>
          <ValueOfContents>4,500.00</ValueOfContents>
          <InsComment>DESTINATION</InsComment>
          <SvcCommitments>6 - 10 Days</SvcCommitments>
          <SvcDescription>Priority Mail International Flat-Rate Envelope</SvcDescription>
        </Service>
        <Service ID="9">
          <Pounds>1</Pounds>
          <Ounces>14</Ounces>
          <MailType>Package</MailType>
          <Country>AUSTRALIA</Country>
          <Postage>41.95</Postage>
          <ValueOfContents>4,500.00</ValueOfContents>
          <InsComment>INSURED VALUE</InsComment>
          <ParcelIndemnityCoverage>70.43</ParcelIndemnityCoverage>
          <SvcCommitments>6 - 10 Days</SvcCommitments>
          <SvcDescription>Priority Mail International Regular/Medium Flat-Rate Boxes</SvcDescription>
        </Service>
      </Package>
    </IntlRateResponse>
    
    Code (markup):
    What I need is for SERVICE ID=1 and SERVICE ID=2 ONLY is the value for "POSTAGE".

    I don't need any of the other Service Levels... but it is important the programming pick up ID=1 and ID=2 and ignore the rest.

    Basically, I guess I need a way to loop through the nodes and if it comes across 1 or 2 for Service, to grab the value for "Postage" and either store each value in a separate variable or an array.

    I am stuck. If someone can help, it'd be greatly appreciated.

    Oh - Package ID will ALWAYS be 0 and will be the only one.
     
    nyxano, Nov 18, 2009 IP
  2. nyxano

    nyxano Peon

    Messages:
    417
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It took me a while but since I knew the ID's were a constant, rather than looping through the entire XML, I just extracted what I needed using the following:

    xmlDoc.selectSingleNode("//IntlRateResponse/Package/Service[@ID='2']/Postage").text

    Probably not the cleanest way of getting the data I needed but it works. I'll still try and find a permanent solution but for now, this accomplishes what I need.
     
    nyxano, Nov 19, 2009 IP