retriving value :

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    retriving value :

    Hey all
    I am working on Shipping module....And i have one XML script which all gives me the below output :

    XML Resposne:
    Code:
     <?xml version="1.0"?>
    <CustomerContext> Shipping1.0001
    <ResponseStatusCode>1
    Success</ResponseStatusDescription>03
    </Code>Your invoice may vary from the displayed reference rates
    <Code>LBS
    </UnitOfMeasurement>55.0
    </BillingWeight>
    <TransportationCharges>USD
    <MonetaryValue>44.59
    <ServiceOptionsCharges>
    <CurrencyCode>USD0.00
    </MonetaryValue>USD
    </CurrencyCode>44.59
    <GuaranteedDaysToDelivery>
    </ScheduledDeliveryTime>
    USD</CurrencyCode>
    <MonetaryValue>44.59
    <ServiceOptionsCharges>USD0.00
    </MonetaryValue>USD
    </CurrencyCode>44.59
    55.0</Weight>LBS
    </UnitOfMeasurement>55.0
    </RatedShipment>
    My question is <TotalCharges>< CurrencyCode>US D</CurrencyCode><M onetaryValue>44 .59
    </MonetaryValue></TotalCharges>
    In the above one , i want to store the value of that MonetaryValue which is 44.59 into one variable.....
    how can i do that...
    just to show , below is the XML which i have used

    Code:
    $this->_xml_request =<<<XML
    <?xml version="1.0"?>
    <AccessRequest xml:lang="en-US">
    <AccessLicenseNumber></AccessLicenseNumber>
    <UserId></UserId>
    <Password></Password>
    </AccessRequest>
    <?xml version="1.0"?>
    <RatingServiceSelectionRequest xml:lang="en-US">
       <Request>
          <TransactionReference>
             <CustomerContext>Shipping</CustomerContext>
             <XpciVersion>1.0001</XpciVersion>
          </TransactionReference>
          <RequestAction>$this->_request_option</RequestAction>
          <RequestOption>$this->_request_option</RequestOption>
       </Request>
       <PickupType>
          <Code></Code>
       </PickupType>
     <Shipment>
          <Shipper>
            <ShipperNumber></ShipperNumber>
            <Address>
            <City></City>
            <StateProvinceCode></StateProvinceCode>
             <PostalCode></PostalCode>
             <CountryCode></CountryCode>
             </Address>
          </Shipper>
          <ShipTo>
             <Address>
            <StateProvinceCode>$state</StateProvinceCode>
                <PostalCode>$postal_code</PostalCode>
                <CountryCode>$country</CountryCode>
    <ResidentialAddress>0</ResidentialAddress>
             </Address>
          </ShipTo>
          <ShipFrom>
             <Address>
            <StateProvinceCode></StateProvinceCode >
                    <PostalCode></PostalCode>
                    <CountryCode></CountryCode>
             </Address>
          </ShipFrom>
          <Service>
             <Code></Code>
          </Service>
          <Package>
             <PackagingType>
                <Code></Code>
             </PackagingType>
             <PackageWeight>
                <UnitOfMeasurement>
                   <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>$weight</Weight>
             </PackageWeight>
          </Package>
          <RateInformation>
      <NegotiatedRatesIndicator/>
          </RateInformation>
       </Shipment>
    </RatingServiceSelectionRequest>
    XML;
    can you help me , how i can store the value ?
  • ajd335
    New Member
    • Apr 2008
    • 123

    #2
    Xml text to php conversion

    Hey all..
    below is the response which i get when i run my script


    XML Resposne:
    Code:
     <?xml version="1.0"?>
    <CustomerContext> Shipping1.0001
    </TransactionReference>
    <ResponseStatusCode>1
    <ResponseStatusDescription>Success
    <RatedShipment>03
    <RatedShipmentWarning>Your invoice may vary from the displayed reference rates
    <UnitOfMeasurement>LBS
    </UnitOfMeasurement>36.0
    </BillingWeight>
    <CurrencyCode>USD
    <MonetaryValue>31.14
    </TransportationCharges>
    <ServiceOptionsCharges>
    <CurrencyCode>USD
    <MonetaryValue>0.00
    </ServiceOptionsCharges>
    <TotalCharges>USD
    <MonetaryValue>31.14
    <GuaranteedDaysToDelivery>
    </GuaranteedDaysToDelivery>
    </ScheduledDeliveryTime>
    <RatedPackage>
    <CurrencyCode>USD
    <MonetaryValue>31.14
    </TransportationCharges>
    <CurrencyCode>USD
    <MonetaryValue>0.00
    </ServiceOptionsCharges>
    <CurrencyCode>USD
    <MonetaryValue>31.1436.0
    <BillingWeight>
    <Code>LBS36.0
    </BillingWeight>
    I want to convert that text into array..actually i wanted to store the value of Monetaryvalue which is 31.14 here into one variable..How cam i do this...

    <TotalCharges>< CurrencyCode>US D</CurrencyCode><M onetaryValue>31 .14</MonetaryValue></TotalCharges>

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      You can read XML files in PHP using SimpleXML.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        For something really simple as this, I would recommend a scripting language to find the regular expression bounded by the start and end tags.
        It's a lot faster, although not as rigorous.

        Comment

        • ajd335
          New Member
          • Apr 2008
          • 123

          #5
          Originally posted by jkmyoung
          For something really simple as this, I would recommend a scripting language to find the regular expression bounded by the start and end tags.
          It's a lot faster, although not as rigorous.
          Hi jkmyoung ,
          yeah may be that will be a good idea...
          I have tried out
          $var = preg_match("/<TotalCharges>< CurrencyCode>US D<\/CurrencyCode><M onetaryValue>^( .*)$<\/MonetaryValue>/
          ",$str,$matches );

          because i wanted the string which is 31.14 (that may change e'time) , but not working
          Do you have any idea, why its not working ??

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by ajd335
            Hi jkmyoung ,
            yeah may be that will be a good idea...
            I have tried out
            $var = preg_match("/<TotalCharges>< CurrencyCode>US D<\/CurrencyCode><M onetaryValue>^( .*)$<\/MonetaryValue>/
            ",$str,$matches );

            because i wanted the string which is 31.14 (that may change e'time) , but not working
            Do you have any idea, why its not working ??
            Your XML file seems to have many MonetaryValue tags, some of them with different values as well.

            Comment

            • ajd335
              New Member
              • Apr 2008
              • 123

              #7
              Originally posted by r035198x
              Your XML file seems to have many MonetaryValue tags, some of them with different values as well.
              Hi,
              I have tried out many XML functions , but it dint work correctly, so i was trying to use regular expression.
              Yes , it does contain more than one monetary value , But i want the Monetary value which is under Total Charges array..
              Code:
              <?xml version="1.0"?><RatingServiceSelectionResponse><Response>
              <TransactionReference>
              <CustomerContext> Shipping</CustomerContext><XpciVersion>1.0001
              </XpciVersion></TransactionReference>
              <ResponseStatusCode>1</ResponseStatusCode>
              <ResponseStatusDescription>Success
              </ResponseStatusDescription></Response>
              <RatedShipment><Service><Code>03</Code></Service>
              <RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning><BillingWeight><UnitOfMeasurement>
              <Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight>
              </BillingWeight><TransportationCharges>
              <CurrencyCode>USD</CurrencyCode>
              <MonetaryValue>31.14</MonetaryValue></TransportationCharges>
              <ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode>
              <MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges>
              <TotalCharges><CurrencyCode>USD</CurrencyCode>
              <MonetaryValue>31.14</MonetaryValue></TotalCharges>
              <GuaranteedDaysToDelivery>
              </GuaranteedDaysToDelivery><ScheduledDeliveryTime>
              </ScheduledDeliveryTime>
              <RatedPackage><TransportationCharges>
              <CurrencyCode>USD</CurrencyCode>
              <MonetaryValue>31.14</MonetaryValue></TransportationCharges>
              <ServiceOptionsCharges><CurrencyCode>USD
              </CurrencyCode><MonetaryValue>0.00</MonetaryValue>
              </ServiceOptionsCharges><TotalCharges>
              <CurrencyCode>USD</CurrencyCode>
              <MonetaryValue>31.14</MonetaryValue></TotalCharges>
              <Weight>36.0</Weight>
              <BillingWeight><UnitOfMeasurement><Code>LBS</Code>
              </UnitOfMeasurement><Weight>36.0</Weight></BillingWeight>
              </RatedPackage></RatedShipment></RatingServiceSelectionResponse>
              I have tried below expression , but dint work...

              Code:
              I tried out
              $str = $_xml_response;
              $var = preg_match("/<TotalCharges><CurrencyCode>
              USD<\/CurrencyCode><MonetaryVa
              lue>^(.*)$<\/MonetaryValue>/",$str,$matches);
              if($var =true){
              echo $matches[0];

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                You realize of course that
                Code:
                if($val = true)
                is not the same as
                Code:
                if($val == true)

                Comment

                • ajd335
                  New Member
                  • Apr 2008
                  • 123

                  #9
                  Originally posted by r035198x
                  You realize of course that
                  Code:
                  if($val = true)
                  is not the same as
                  Code:
                  if($val == true)
                  hi,
                  yeah , It was by mistake , but still my pregmatch statment is showing no matches ...do you have any idea . how to solve so?
                  Thanks ,

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by ajd335
                    hi,
                    yeah , It was by mistake , but still my pregmatch statment is showing no matches ...do you have any idea . how to solve so?
                    Thanks ,
                    What is the current output of your program?

                    Comment

                    • ajd335
                      New Member
                      • Apr 2008
                      • 123

                      #11
                      Originally posted by r035198x
                      What is the current output of your program?
                      hi,
                      My XML_response from UPS API is (the output )
                      Code:
                      <?xml version="1.0"?><RatingServiceSelectionResponse><Response><TransactionReference><CustomerContext> Shipping</CustomerContext>
                      <XpciVersion>1.0001</XpciVersion></TransactionReference>
                      <ResponseStatusCode>1</ResponseStatusCode>
                      <ResponseStatusDescription>Success</ResponseStatusDescription>
                      </Response><RatedShipment><Service><Code>03</Code></Service><RatedShipmentWarning>Your invoice may vary from the displayed reference rates</RatedShipmentWarning><BillingWeight><UnitOfMeasurement>
                      <Code>LBS</Code></UnitOfMeasurement><Weight>36.0</Weight></BillingWeight>
                      <TransportationCharges><CurrencyCode>USD</CurrencyCode>
                      <MonetaryValue>31.14</MonetaryValue></TransportationCharges>
                      <ServiceOptionsCharges><CurrencyCode>USD</CurrencyCode>
                      <MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges>
                      <TotalCharges><CurrencyCode>
                      USD</CurrencyCode><MonetaryValue>31.14</MonetaryValue>
                      </TotalCharges><GuaranteedDaysToDelivery></GuaranteedDaysToDelivery>
                      <ScheduledDeliveryTime></ScheduledDeliveryTime>
                      <RatedPackage><TransportationCharges>
                      <CurrencyCode>USD</CurrencyCode>
                      <MonetaryValue>31.14</MonetaryValue>
                      </TransportationCharges><ServiceOptionsCharges>
                      <CurrencyCode>USD</CurrencyCode>
                      <MonetaryValue>0.00</MonetaryValue></ServiceOptionsCharges>
                      <TotalCharges><CurrencyCode>USD</CurrencyCode>
                      <MonetaryValue>31.14</MonetaryValue></TotalCharges>
                      <Weight>36.0</Weight><BillingWeight>
                      <UnitOfMeasurement>
                      <Code>LBS</Code>
                      </UnitOfMeasurement><Weight>36.0</Weight>
                      </BillingWeight></RatedPackage></RatedShipment></RatingServiceSelectionResponse>
                      and using the preg_match as


                      Code:
                      $str = $shipping->_xml_response;
                      
                      $var = preg_match("/<TotalCharges><CurrencyCode>USD<\/CurrencyCode><MonetaryValu
                      e>(.*)<\/MonetaryValue>/",$str,$matches);
                      if($var == true){
                      echo $matches[0];}else echo "no match";
                      using above preg_match I am getting o/p as USD31.14USD31.1 4USD0.00USD31.1 4
                      and i just wanted to have 31.14 $......the last 31.14 (not the first one, not the 0.00 but the last 31.14 )...thanks

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        If you know that it's always the last one, then why not snip it off from the returned value.

                        Comment

                        • ajd335
                          New Member
                          • Apr 2008
                          • 123

                          #13
                          Originally posted by r035198x
                          If you know that it's always the last one, then why not snip it off from the returned value.

                          Hi ,
                          I have made some changes in perg_match statement and now its working fine..
                          Thanks.

                          Comment

                          Working...