bravo14 Posted March 16, 2011 Share Posted March 16, 2011 Hi I am trying to use SimpleXML to populate some form fields from an XML file This is the SimpleXML code that I am using <?php $vrm=strtoupper($_POST['veh_reg']); $file="https://www.cdlvis.com/lookup/getxml?username=username&mode=test&key=key&vrm=".$vrm.""; $dom=new DOMdocument(); $dom->load($file); $xml=simplexml_import_dom($dom); var_dump($xml); ?> The code for the display of the fields is: <form name="vehicle_details" method="post" action="add_vehicle.php"> <table> <tr> <td>Registration Number:</td><td><input name="reg_num" type="text" value="<?php echo $xml->result[0]->vrm;?>" readonly="true" /></td></tr> <tr> <td>Make:</td><td><input name="make" type="text" value="<?php echo $xml->result[0]->make;?>" readonly="true" /></td></tr> <tr> <td>Model:</td><td><input name="model" type="text" value="<?php echo $xml->result[0]->model;?>" readonly="true" /></td></tr> <tr> <td>Date of Manufacture:</td><td><input name="date_man" type="text" value="<?php echo $xml->result[0]->date_manufactured;?>" readonly="true" /></td></tr> <tr> <td>Date of First Registration:</td><td><input name="date_reg" type="text" value="<?php echo $xml->result[0]->first_registered;?>" readonly="true" /></td></tr> <tr> <td>Colour:</td><td><input name="colour" type="text" value="<?php echo $xml->result[0]->colour;?>" readonly="true" /></td></tr> <tr> <td>Body Style:</td><td><input name="body" type="text" value="<?php echo $xml->result[0]->body;?>" readonly="true" /></td></tr> <tr> <td>Number of Doors:</td><td><input name="doors" type="text" value="<?php echo $xml->result[0]->doors;?>" readonly="true" /></td></tr> <tr> <td>Engine Size:</td><td><input name="engine_size" type="text" value="<?php echo $xml->result[0]->engine_size;?>" readonly="true" /></td></tr> <tr> <td>Fuel Type:</td><td><input name="fuel_type" type="text" value="<?php echo $xml->result[0]->fuel;?>" readonly="true" /></td></tr> <tr> <td>Gearbox Type:</td><td><input name="gearbox" type="text" value="<?php echo $xml->result[0]->gearbox_type;?>" readonly="true" /></td></tr> <tr> <td>Previous Keepers:</td><td><input name="keepers" type="text" value="<?php echo $xml->result[0]->previous_keepers;?>" readonly="true" /></td></tr> <tr> <td>BHP:</td><td><input name="bhp" type="text" value="<?php echo $xml->result[0]->smmt_power_bhp;?>" readonly="true" /></td></tr> <tr> <td>Emissions:</td><td><input name="co2" type="text" value="<?php echo $xml->result[0]->smmt_co2;?>" readonly="true" /></td></tr> <tr> <td><input name="try_again" type="button" value="Try Again" /></td><td><input name="submit" type="submit" value="Next Stage" /></td></tr> </table> </form> In each of the fields on the form when I run the code is the following error Notice: Trying to get property of non-object in I have also done a var_dump of the xml and I get the following object(SimpleXMLElement)#2 (15) { ["@attributes"]=> array(4) { ["id"]=> string(6) "344571" ["generated"]=> string(10) "1300307651" ["mode"]=> string(4) "test" ["account_id"]=> string(3) "165" } ["vrm"]=> string(7) "DC70XSC" ["make"]=> string(7) "RENAULT" ["model"]=> string(23) "SCENIC EXPRESSION 16V A" ["date_manufactured"]=> string(10) "2004-02-20" ["first_registered"]=> string(10) "2004-02-20" ["colour"]=> string(5) "GREEN" ["body"]=> string(3) "MPV" ["doors"]=> string(7) "5 DOORS" ["engine_size"]=> string(4) "1598" ["fuel"]=> string(6) "PETROL" ["gearbox_type"]=> string(19) "TIPTRONIC AUTOMATIC" ["previous_keepers"]=> string(1) "1" ["smmt_power_bhp"]=> string(5) "115.1" ["smmt_co2"]=> object(SimpleXMLElement)#3 (0) { } } What have I done wrong, any help will be much appreciated. Link to comment https://forums.phpfreaks.com/topic/230851-simplexml-error/ Share on other sites More sharing options...
btherl Posted March 16, 2011 Share Posted March 16, 2011 Instead of var_dump($xml); can you try this please: print "<pre>"; var_dump($xml); print "</pre>"; That will make the structure much clearer. Link to comment https://forums.phpfreaks.com/topic/230851-simplexml-error/#findComment-1188399 Share on other sites More sharing options...
bravo14 Posted March 16, 2011 Author Share Posted March 16, 2011 As requested, structure is below object(SimpleXMLElement)#2 (15) { ["@attributes"]=> array(4) { ["id"]=> string(6) "344691" ["generated"]=> string(10) "1300313095" ["mode"]=> string(4) "test" ["account_id"]=> string(3) "165" } ["vrm"]=> string(7) "DC70XSC" ["make"]=> string(7) "RENAULT" ["model"]=> string(23) "SCENIC EXPRESSION 16V A" ["date_manufactured"]=> string(10) "2004-02-20" ["first_registered"]=> string(10) "2004-02-20" ["colour"]=> string(5) "GREEN" ["body"]=> string(3) "MPV" ["doors"]=> string(7) "5 DOORS" ["engine_size"]=> string(4) "1598" ["fuel"]=> string(6) "PETROL" ["gearbox_type"]=> string(19) "TIPTRONIC AUTOMATIC" ["previous_keepers"]=> string(1) "1" ["smmt_power_bhp"]=> string(5) "115.1" ["smmt_co2"]=> object(SimpleXMLElement)#3 (0) { } } however the structure of the xml file if you go to the url is <?xml version="1.0" encoding="ISO-8859-1" ?> - <result id="343329" generated="1300269146" mode="test" account_id="165"> <vrm>DC70XSC</vrm> <make>RENAULT</make> <model>SCENIC EXPRESSION 16V A</model> <date_manufactured>2004-02-20</date_manufactured> <first_registered>2004-02-20</first_registered> <colour>GREEN</colour> <body>MPV</body> <doors>5 DOORS</doors> <engine_size>1598</engine_size> <fuel>PETROL</fuel> <gearbox_type>TIPTRONIC AUTOMATIC</gearbox_type> <previous_keepers>1</previous_keepers> <smmt_power_bhp>115.1</smmt_power_bhp> <smmt_co2 /> </result> Link to comment https://forums.phpfreaks.com/topic/230851-simplexml-error/#findComment-1188403 Share on other sites More sharing options...
btherl Posted March 16, 2011 Share Posted March 16, 2011 Try accessing $xml->make, $xml->model, etc etc. SimpleXML is exactly that - simple. It may not accurately represent the original XML structure but it works most of the time, and makes the resulting structure simpler in those cases that it does work. Link to comment https://forums.phpfreaks.com/topic/230851-simplexml-error/#findComment-1188407 Share on other sites More sharing options...
bravo14 Posted March 16, 2011 Author Share Posted March 16, 2011 Thank you, that has worked a treat Link to comment https://forums.phpfreaks.com/topic/230851-simplexml-error/#findComment-1188411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.