Jump to content

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>
Edited by kawaii1001
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.');
}
Edited by jazzman1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.