Parsing XML with PHP

Discussion in 'XML & RSS' started by jeanmichel, Apr 12, 2010.

  1. #1
    Hi guys, ive posted a topic on this before regarding how to parse XML using a php script, i have been told to try using an existing function within PHP xmlparse.

    The problem im having is that the XML content gets read in by php then i convert it into an array-this then becomes a nested array-and the real problem is looping through the array getting relevant information out of it so it can be displayed in HTML.

    Just been trying to accomplish something but am struggling to get started-i think i just need a kick in the right direction with it.

    here's a portion of the XML file;
    
    <OTA_VehAvailRateRS xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05" Version="2.004" Target="Test" PrimaryLangID="EN" TimeStamp="2010-04-12T17:35:50.328+01:00"><Success/><VehAvailRSCore><VehRentalCore PickUpDateTime="2010-07-05T10:00:00Z" ReturnDateTime="2010-07-09T10:00:00Z"><PickUpLocation CodeContext="CARTRAWLER" LocationCode="71" Name="Faro - Airport"/><ReturnLocation CodeContext="CARTRAWLER" LocationCode="71" Name="Faro - Airport"/></VehRentalCore><VehVendorAvails><VehVendorAvail><Vendor Code="71:139" CodeContext="CARTRAWLER"/><VehAvails><VehAvail><VehAvailCore Status="Available"><Vehicle AirConditionInd="true" TransmissionType="Manual" FuelType="Diesel" DriveType="Unspecified" PassengerQuantity="5" BaggageQuantity="3" Code="DDMD" CodeContext="CARTRAWLER"><VehType VehicleCategory="1" DoorCount="4-5"/><VehClass Size="35"/><VehMakeModel Name="BMW 1 Series or similar" Code="DDMD"/><PictureURL>https://www.cartrawler.com/otaimages/bmw/1serie_nologo.jpeg</PictureURL><VehIdentity VehicleAssetNumber="5153"/></Vehicle><RentalRate><VehicleCharges><VehicleCharge Description="Breakdown assistance" TaxInclusive="true" IncludedInRate="true" Purpose="602.VCP.X"/><VehicleCharge Description="Airport fee" TaxInclusive="true" IncludedInRate="true" Purpose="30"/><VehicleCharge Description="Tax" IncludedInRate="true" Purpose="7"/><VehicleCharge Description="Unlimited mileage" TaxInclusive="true" IncludedInRate="true" Purpose="609.VCP.X"/></VehicleCharges><RateQualifier RateQualifier="PARTPAID-IN"/></RentalRate><TotalCharge RateTotalAmount="654.67" EstimatedTotalAmount="654.67" CurrencyCode="GBP"/><PricedEquips><PricedEquip><Equipment EquipType="9"><Description>Booster seat</Description></Equipment><Charge Amount="21.05" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="8"><Description>Child toddler seat</Description></Equipment><Charge Amount="21.05" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="7"><Description>Infant child seat</Description></Equipment><Charge Amount="21.05" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="13"><Description>GPS - Satellite Navigational System</Description></Equipment><Charge Amount="33.32" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="101.EQP"><Description>Additional Driver</Description></Equipment><Charge Amount="12.28" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip></PricedEquips><Fees><Fee Amount="344.69" CurrencyCode="GBP" Purpose="22"/><Fee Amount="309.99" CurrencyCode="GBP" Purpose="23"/><Fee Amount="0.00" CurrencyCode="GBP" Purpose="6"/></Fees><Reference Type="16" ID="1564421314" ID_Context="CARTRAWLER" DateTime="2010-04-12T17:35:50.328+01:00" URL="CT#pOE4n6silz7MZSIkazaGO01OzxreVnGbHsy1t1s7RDHcImVGYx9ghwa2myUAVjQu3VJebeziQyRKm15Idn7ZvZfnunnjRG2fbE7l3EwXPPk4FYYaUmmrAlkwLbgcBx0BdIpJXM--PYTlkACEPd28va1kkrOQkcxDWEuw3OVStvqPLpxnG7VBFGok4OU10MjRVMqfs22CFqUnMEns8G_9hbtuTtLCQiueDXqJHY621lx3x95CE25emExja-CQVG4Dm2rEz0V4Wmd3aqX6dAzHBt5aDLHgJTScwtAkThTuEAOnJTibK9JzMmZP-cyDg3TtSgB7YtWDV-qAPINp-qORpwIakk5BelMywxRN_I6bZAwb63x8inEKQ3GE8dRh6pxmS_M1X_jvVP6Awb1ZJ46k9XGE8dRh6pxmOIKfJRhlQae_xtGmb22Uhm0Q35iywHyy30UWAylDkrxupqQr83_4bVwuXEM4vYNh_B3g5scr3Ffsh8gofP_wzHjhCX0Y6UBqcYTx1GHqnGbSa2-mnyJ31U8O7R0aLNKpzxMDzvJaTiG7ysywr6sZFXDcHk4s_DGZUkikf_OEF1rRGwEd2SxAQn482Q0e7mRz-yQDv1_OrwLLVemlGFIhI5CkdyqAlxdeR4x0D2Rhaw7OcrVV6NwLe6hnWamki5SPvkYfFJ0ANWbcitUBx7UlhaVEtWaq1EQn2oh8wyjgBv_mqdS-pKNqf5TVYgJvmopnFQCkDDOlO9Abni7vf5fin4VGNInE3HvCfBOP5PbYlgYS_lPNw6wBqyoCAr9gFCTb3snz9gcfK8MsCAYxe9054RvVvmlvLMY5rRjEdi8nTcTTLB6H5bfgVj8w3NxFb4M4S6l975OQHv4="/><TPA_Extensions><OrderBy Index="48"/><CC_Info Required="true"/><Duration Days="4"/><Insurance avail="true"/><Fees><Fee Amount="393.15" CurrencyCode="EUR" Purpose="22"/><Fee Amount="353.57" CurrencyCode="EUR" Purpose="23"/></Fees><CurrencyExchange Rate="1.140608" Rate23="0.876726"/></TPA_Extensions></VehAvailCore><VehAvailInfo><PricedCoverages><PricedCoverage><Coverage CoverageType="6"/><Charge Description="Collision damage waiver (CDW)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage><PricedCoverage><Coverage CoverageType="47"/><Charge Description="Theft waiver (TW)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage><PricedCoverage><Coverage CoverageType="50"/><Charge Description="Third party liability protection (TP)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage></PricedCoverages></VehAvailInfo></VehAvail></VehAvails><Info><LocationDetails AtAirport="1" Code="71" Name="Faro - Airport"><Address><AddressLine>FARO AIRPORT, FARO, 8006-701</AddressLine><CountryName Code="PT"/></Address><Telephone PhoneNumber="351 289 818726"/><AdditionalInfo><VehRentLocInfos><VehRentLocInfo Type="VLI_3.VLI.X"/></VehRentLocInfos></AdditionalInfo></LocationDetails><TPA_Extensions><Ranking index="1000"/><BookEngine layout="GRID_TOTAL" compass="UNKNOWN" marketingEmail="1" processCC="1" dateFormat="dd/mm/yyyy"/></TPA_Extensions></Info></VehVendorAvail><VehVendorAvail><Vendor Code="71:189" CodeContext="CARTRAWLER"/><VehAvails><VehAvail><VehAvailCore Status="Available"><Vehicle AirConditionInd="true" TransmissionType="Manual" FuelType="Petrol" DriveType="Unspecified" PassengerQuantity="5" BaggageQuantity="5" Code="IWMR" CodeContext="CARTRAWLER"><VehType VehicleCategory="8" DoorCount="5"/><VehClass Size="6"/><VehMakeModel Name="Ford Mondeo Stationwagon or similar" Code="IWMR"/><PictureURL>https://www.cartrawler.com/otaimages/ford/mondeo_estate_nologo.jpeg</PictureURL><VehIdentity VehicleAssetNumber="5969"/></Vehicle><RentalRate><VehicleCharges><VehicleCharge Description="Breakdown assistance" TaxInclusive="true" IncludedInRate="true" Purpose="602.VCP.X"/><VehicleCharge Description="Airport fee" TaxInclusive="true" IncludedInRate="true" Purpose="30"/><VehicleCharge Description="Tax" IncludedInRate="true" Purpose="7"/><VehicleCharge Description="Unlimited mileage" TaxInclusive="true" IncludedInRate="true" Purpose="609.VCP.X"/></VehicleCharges><RateQualifier RateQualifier="PARTPAID-IN"/></RentalRate><TotalCharge RateTotalAmount="435.91" EstimatedTotalAmount="435.91" CurrencyCode="GBP"/><PricedEquips><PricedEquip><Equipment EquipType="9"><Description>Booster seat</Description></Equipment><Charge Amount="10.53" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="8"><Description>Child toddler seat</Description></Equipment><Charge Amount="10.53" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="13"><Description>GPS - Satellite Navigational System</Description></Equipment><Charge Amount="35.07" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="101.EQP"><Description>Additional Driver</Description></Equipment><Charge Amount="5.27" CurrencyCode="GBP" TaxInclusive="true" IncludedInRate="false"/></PricedEquip></PricedEquips><Fees><Fee Amount="229.51" CurrencyCode="GBP" Purpose="22"/><Fee Amount="206.41" CurrencyCode="GBP" Purpose="23"/><Fee Amount="0.00" CurrencyCode="GBP" Purpose="6"/></Fees><Reference Type="16" ID="1491479516" ID_Context="CARTRAWLER" DateTime="2010-04-12T17:35:50.328+01:00" URL="CT-ghlq07qtetF8s7PhQdqVneAhn3vIoi9uBVORhb6VvxXx9l0HwymrDquDk92lItXNOdm-Z-QHd9_zg90_W1h1R9Czmog_MhG36ECLrnTFiup4GuQ4G5jZAtR5A4VgHfAGnosX7yaektX-JXgPkBDNzzVYb6al4O9ex0NDUnkYSAQX729s0bXqczTW0gVaHn6E0p2y22G1GuaafAGO4thMlM97CfAAhxO5-uPZeEOZBDd6O7GMFp__pf9kVDjT9X4AemdCqFXtvo_sh8gofP_wzEtwPvF-cnWlVP3Z3xg2tinsh8gofP_wzHIdZe1Pp5P9Es-sN5NySYklylDOYOb2RuGgSYvA9XbqJ6fcJlnn_AlTpiSyTbozGnGE8dRh6pxm0mtvpp8id9VEVheW8tZRduyHyCh8__DMRZdkb65naS4YIaav0dV42MXY5LPONXrPRaZiKDh5bCGyIvx6QV5LRU9yQzyC5Qhi_xYIRDV7V5UWN-bQspj3XVAtmFRvHGy8cEPyk5zdX1gcFWQwJ5HGOX5L5Bf0Q1n-ky9BgbNv45BfUlEsknjFUpoAypjbHH0oF8jKM4BtJSPK9RT_ytU9EZR23H3OA_W-k27rRs5S4B1JyJMn2Kt5nck82BbZsZH7nd7fBwt5lX952_GYV6aEcJWkxCGuEI1DzneYSu7CwK2FRjSJxNx7wnwTj-T22JYGPaoZs8ueFk7XKazg19zuandJe-IBdOrTShC9ZDmlKYZsFGOPnmvZ-7DG3x56wZeR"/><TPA_Extensions><OrderBy Index="29"/><CC_Info Required="true"/><Duration Days="4"/><Insurance avail="true"/><Fees><Fee Amount="261.77" CurrencyCode="EUR" Purpose="22"/><Fee Amount="235.43" CurrencyCode="EUR" Purpose="23"/></Fees><CurrencyExchange Rate="1.140608" Rate23="0.876726"/></TPA_Extensions></VehAvailCore><VehAvailInfo><PricedCoverages><PricedCoverage><Coverage CoverageType="6"/><Charge Description="Collision damage waiver (CDW)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage><PricedCoverage><Coverage CoverageType="47"/><Charge Description="Theft waiver (TW)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage><PricedCoverage><Coverage CoverageType="50"/><Charge Description="Third party liability protection (TP)" TaxInclusive="true" IncludedInRate="true"/></PricedCoverage></PricedCoverages></VehAvailInfo></VehAvail><VehAvail><VehAvailCore Status="Available"><Vehicle AirConditionInd="true" TransmissionType="Manual" FuelType="Unspecified" DriveType="Unspecified" PassengerQuantity="9" BaggageQuantity="4" Code="LVMR" CodeContext="CARTRAWLER"><VehType VehicleCategory="2" DoorCount="4"/><VehClass Size="11"/><VehMakeModel Name="Mercedes Sprinter - 9 seats or similar" Code="LVMR"/><PictureURL>https://www.cartrawler.com/otaimages/mercedes_benz/sprinter_nologo.jpg</PictureURL><VehIdentity VehicleAssetNumber="6497"/></Vehicle><RentalRate><VehicleCharges><VehicleCharge Description="Breakdown assistance" TaxInclusive="true" IncludedInRate="true" Purpose="602.VCP.X"/><VehicleCharge Description="Airport fee" TaxInclusive="true" IncludedInRate="true" Purpose="30"/><VehicleCharge Description="Tax" IncludedInRate="true" Purpose="7"/><VehicleCharge Description="Unlimited mileage" TaxInclusive="true" IncludedInRate="true" Purpose="609.VCP.X"/></VehicleCharges><RateQualifier RateQualifier="PARTPAID-IN"/></RentalRate><TotalCharge RateTotalAmount="461.31" EstimatedTotalAmount="461.31" CurrencyCode="GBP"/><PricedEquips><PricedEquip><Equipment EquipType="9"><Description>Booster seat</Description></Equipment><Charge Amount="10.53" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="8"><Description>Child toddler seat</Description></Equipment><Charge Amount="10.53" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="13"><Description>GPS - Satellite Navigational System</Description></Equipment><Charge Amount="35.07" CurrencyCode="GBP" TaxInclusive="false" IncludedInRate="false"/></PricedEquip><PricedEquip><Equipment EquipType="101.EQP"><Description>Additional Driver</Description></Equipment><Charge Amount="5.27" CurrencyCode="GBP" TaxInclusive="true" IncludedInRate="false"/></PricedEquip></PricedEquips><Fees><Fee Amount="242.88" CurrencyCode="GBP" Purpose="22"/><Fee Amount="218.43" CurrencyCode="GBP" Purpose="23"/><Fee Amount="0.00" CurrencyCode="GBP" Purpose="6"/></Fees>
    
    Code (markup):

     
    jeanmichel, Apr 12, 2010 IP
  2. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Use PHP's simple xml functions to parse the xml file as it's much each when compared with DOM XML and SAX Parsers.
     
    kajol, Apr 12, 2010 IP
  3. angelomaniac

    angelomaniac Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    angelomaniac, Apr 14, 2010 IP
  4. jeanmichel

    jeanmichel Peon

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks guys but am getting absolutely nowhere with this-is there anybody who wants a quick $200 for what is potentially 5 minutes work?-all i need is a way of extracting info from an XML string in an ordered fashion such as;

    
    
    $xml = simplexml_load_string($xml);
    
    if (count($xml->VehAvails) > 0) {
    
        foreach ($xml->VehAvails $node) {
    
            // This prints out each of the models
    
            echo $node->detail.chr(100);
    
        }
    
    }
    
    
    Code (markup):
    It's slightly more complicated because the XML string i'm working with is extremely nested, that's where i think i'm in too deep.

    Any takers?
     
    jeanmichel, Apr 14, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    You are not parsing the XML nodes correctly. "VehAvails" appears to be nested inside other nodes.

    What exactly are you trying to return from the XML? It doesnt sound too difficult, and i certainly wont ask for $200!!

    Also, how large is the XML? Simple XML loads the document into memory so may not be suitable for large documents.
     
    lukeg32, Apr 15, 2010 IP
  6. jeanmichel

    jeanmichel Peon

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi luke-the xml is about 90kb roughly-i could recieve it in .gz but i don't think it makes a difference when it has to be loaded using simplexml_load_string().

    you've hit the nail on the head exactly, the VehAvails is nested inside other nodes, but how do i get access to data which is this nested, is it as simple as example:

    $xml->VehSuccess->VehDetail->VehAvails

    ??

    I know it seems like a double quick job-i know i can do it but i end up wanting to smash the laptop up after 10 minutes of persisting! PM me your details dude-if you can help me, you're in the UK right? Im in Manchester. I know the payment for such a job seems a lot but this is indicative of how appriciative i would be to solving this issue.
     
    jeanmichel, Apr 15, 2010 IP
  7. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #7
    jeanmichel,

    I will send you a PM - i can probably do this in a couple of minutes for you, though it would help to have the XML.....
     
    lukeg32, Apr 15, 2010 IP
  8. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Why do you need to convert the content of xml into an array. XML is already a very well structure database. Just use xpath.
     
    unigogo, Apr 18, 2010 IP