Jump to content

Help with xml parsing using php


kawaii1001

Recommended Posts

I am very basic with php and am needed to parse an xml file.  I have completed to where it is pulling some parts, but isn't pulling the children, and I am uncertain how to grab those.  This is what I have:

 

<?php
    $preowned = simplexml_load_file('File.xml');
     foreach ($preowned as $preownedinfo):
    $ADID=$preownedinfo->ADID;
    $CompanyID=$preownedinfo->CompanyID;
    $Category=$preownedinfo->Category;
    $StockNumber=$preownedinfo->StockNumber;
        $Year=$preownedinfo->Yrs;
        $Make=$preownedinfo->Make;
        $Model=$preownedinfo->Model;
        $Trim=$preownedinfo->Trim;
        $Odometer=$preownedinfo->Odometer;
        $ExteriorColor=$preownedinfo->ExteriorColor;
        $InteriorColor=$preownedinfo->InteriorColor;
        $Engine=$preownedinfo->Engine;
        $Transmission=$preownedinfo->Transmission;
        $Doors=$preownedinfo->Doors;
        $Price=$preownedinfo->Price;
        $Options=$preownedinfo->Options;
        $AdDescription=$preownedinfo->AdDescription;
        $MainPhoto=$preownedinfo->MainPhoto;
        $AdditionalPhoto=$preownedinfo->AdditionalPhoto;
        $Doors=$preownedinfo->Doors;
    
echo $Year $Make $Doors $Model;
      endforeach;
?>       
 
 
Here is the xml:
<root><AD><ADID displayName="ADID">1111</ADID><CompanyID displayName="CompanyID">0000</CompanyID><CompanyName displayName="CompanyName">Company Name</CompanyName><Category displayName="Category">Sport Utility</Category><StockNumber displayName="StockNumber">999999</StockNumber><Vin displayName="Vin">22222</Vin><Status displayName="Status">Used</Status><Yrs displayName="Year">2010</Yrs><Make displayName="Make">Lincoln</Make><Model displayName="Model">Navigator</Model><Trim displayName="Trim">Sport Utility</Trim><ExtraField displayName="ExtraField"><ContentEN><Odometer>47610</Odometer><ExteriorColor>White</ExteriorColor><InteriorColor>Black</InteriorColor><FuelType>Flexible</FuelType><Drive>AWD</Drive><Engine>V8 Flex Fuel 5.4L</Engine><Transmission>Automatic</Transmission><Doors>4</Doors></ContentEN></ExtraField><Price displayName="Price">44900.0000</Price><HidePrice displayName="HidePrice">0</HidePrice><Options displayName="Options">AM/FM Stereo, Air Conditioning</Options><AdDescription displayName="AdDescription">AC Seats,Heated Seats,Memory Seats,Wood Trim</AdDescription><FinancingIsAvailable displayName="FinancingIsAvailable">0</FinancingIsAvailable><FinancingPayment displayName="FinancingPayment" /><FinancingPaymentType displayName="FinancingPaymentType" /><FinancingNumberOfPayment displayName="FinancingNumberOfPayment" /><FinancingDownPayment displayName="FinancingDownPayment" /><FinancingSource displayName="FinancingSource" /><FinancingType displayName="FinancingType" /><FinancingOdometer displayName="FinancingOdometer" /><FinancingDescription displayName="FinancingDescription" /><ManufactureProgram displayName="ManufactureProgram" /><Warranty displayName="Warranty" /><WarrantyDescription displayName="WarrantyDescription" /><MainPhoto displayName="MainPhoto">http://www.photo.jpg</MainPhoto><AditionalPhotos displayName="AditionalPhotos"><AditionalPhoto>http://www.photo.jpg</AditionalPhoto></AditionalPhotos><ModifiedDate displayName="ModifiedDate">06/18/2013 15:17:17</ModifiedDate><CreatedDate displayName="CreatedDate">03/30/2011 21:47:53</CreatedDate></AD><AD><ADID displayName="ADID">111111</ADID></AD></root>
Link to comment
https://forums.phpfreaks.com/topic/281540-help-with-xml-parsing-using-php/
Share on other sites

Debug your xml data outputs and turn on error_reporting on the top of the file:

 

This is wrong php syntax - echo $Year $Make $Doors $Model;

 

Use code tags in the future!

 

Try, (uncomment my echo's)

<?php

error_reporting(-1);

if (file_exists('File.xml')) {
   $preowned = simplexml_load_file('File.xml');
     foreach ($preowned as $preownedinfo):
    $ADID=$preownedinfo->ADID;
    $CompanyID=$preownedinfo->CompanyID;
    $Category=$preownedinfo->Category;
    $StockNumber=$preownedinfo->StockNumber;
        $Year=$preownedinfo->Yrs;
        $Make=$preownedinfo->Make;
        $Model=$preownedinfo->Model;
        $Trim=$preownedinfo->Trim;
        $Odometer=$preownedinfo->Odometer;
        $ExteriorColor=$preownedinfo->ExteriorColor;
        $InteriorColor=$preownedinfo->InteriorColor;
        $Engine=$preownedinfo->Engine;
        $Transmission=$preownedinfo->Transmission;
        $Price=$preownedinfo->Price;
        $Options=$preownedinfo->Options;
        $AdDescription=$preownedinfo->AdDescription;
        $MainPhoto=$preownedinfo->MainPhoto;
        $AdditionalPhoto=$preownedinfo->AdditionalPhoto;
        $Doors=$preownedinfo->ExtraField->Doors;
        
       // echo '<pre>'.print_r($preownedinfo->ADID,true).'</pre>';
        
       // echo '<pre>'.print_r($preownedinfo->ExtraField,true).'</pre>';
       
       //echo '<pre>'.print_r($preownedinfo->ExtraField->ContentEN, true).'</pre>';
      
      endforeach;

} else {
    exit('Failed to open File.xml.');
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.