Announcement

Collapse
No announcement yet.

Am I parsing this XML correctly?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Am I parsing this XML correctly?

    I'm writing a shipping module that requests rates from FedEx etc. I'm able to retrieve FedEx's XML rate response, and now I'm parsing through it to get the rates. Is looping through the children the only way to examine the results? This is not fun

    For example here's a much-shortened and simplified XML blurb. Basically there's some misc info toward the top, and then a bunch of `RateReplyDetails` tags, each with a method and a price, among many other things that I won't need.

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <RateReply xmlns="http://fedex.com/ws/rate/v28">
    <HighestSeverity>SUCCESS</HighestSeverity>
    <RateReplyDetails>
    <ServiceType>FIRST_OVERNIGHT</ServiceType>
    <ServiceDescription></ServiceDescription>
    <PackagingType>YOUR_PACKAGING</PackagingType>
    <SignatureOption>SERVICE_DEFAULT</SignatureOption>
    <RatedShipmentDetails>
    <ShipmentRateDetail>
    <TotalBaseCharge>
    <Currency>USD</Currency>
    <Amount>230.92</Amount>
    </TotalBaseCharge>
    <TotalFreightDiscounts>
    <Currency>USD</Currency>
    <Amount>0.0</Amount>
    </TotalFreightDiscounts>
    <TotalNetFreight> ... </TotalNetFreight>
    <TotalSurcharges> ... </TotalSurcharges>
    <TotalNetFedExCharge>
    <Currency>USD</Currency>
    <Amount>284.04</Amount>
    </TotalNetFedExCharge>
    <TotalTaxes> ... </TotalTaxes>
    <TotalNetCharge> ... </TotalNetCharge>
    <TotalRebates> ... </TotalRebates>
    <TotalDutiesAndTaxes> ... </TotalDutiesAndTaxes>
    <TotalAncillaryFeesAndTaxes> ... </TotalAncillaryFeesAndTaxes>
    <TotalDutiesTaxesAndFees> ... </TotalDutiesTaxesAndFees>
    <TotalNetChargeWithDutiesAndTaxes>
    <Currency>USD</Currency>
    <Amount>284.04</Amount>
    </TotalNetChargeWithDutiesAndTaxes>
    <Surcharges>
    <SurchargeType>FUEL</SurchargeType>
    <Description>Fuel</Description>
    <Amount>
    <Currency>USD</Currency>
    <Amount>53.12</Amount>
    </Amount>
    </Surcharges>
    </ShipmentRateDetail>
    </RatedShipmentDetails>
    </RateReplyDetails>
    
    <RateReplyDetails>
    ...
    </RateReplyDetails>
    
    <RateReplyDetails>
    ...
    </RateReplyDetails>
    
    <RateReplyDetails>
    ...
    </RateReplyDetails>
    </RateReply>
    And I'm parsing that like so:

    Code:
    <mvAssign name="l.parseSuccessful" value="{xml_parse_var( l.response, l.parsedResponse )}">
    For starters, I'd love it if I could simply check `l.parsedResponse:RateReply:HighestSeverity` to see if the request was a success or not, but it doesn't seem like I can. Both of these attempts come up empty:

    Code:
    <br>test 1:<br> <mvEval expr="{l.parsedResponse:RateReply:HighestSeverity} ">
    <br>test 2:<br> <mvEval expr="{l.parsedResponse:RateReply:HighestSeverity:value}">
    It seems like my only option is to loop through children, find the one I want by name, then loop through it's children, find the next one, and so on. I have a ridiculous nested foreach that's just going to get worse as I dig deeper into the XML.

    Code:
    <mvForEach array="l.parsedResponse:RateReply:Children" iterator="l.childA">
      <mvIf expr="{l.childA:name EQ 'RateReplyDetails'}">
        <mvForEach array="l.childA:Children" iterator="l.childB">
          <mvIf expr="{l.childB:name EQ 'RatedShipmentDetails'}">
            <mvForEach array="l.childB:Children" iterator="l.childC">
              <mvIf expr="{l.childC:name EQ 'ShipmentRateDetail'}">
                kill me now
              </mvIf>
            </mvForEach>
          </mvIf>
        </mvForEach>
      </mvIf>
    </mvForEach>
    Is this how it's done in Mivascript, or am I missing something totally obvious?
    Last edited by Mike521w; 05-27-22, 11:06 AM.
    Looking for work as of March 2024! I've been a web developer for going on 20 years, with most of that time spent on Miva sites.

    #2
    This should help

    https://www.miva.com/forums/forum/de...rsing-xml-data
    Bruce Golub
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment

    Working...
    X