xml - PHP SimpleXML, get line of child during foreach loop -


i have script goes through simplexml object loaded file saved on server. now, if something's wrong while process xml file, want script report error, , preferably line of child error occurred.

an example of xml:

<xml>   <product>     <name>product one</name>     <price>3.50</price>   </product>   <product>     <name>product two</name>     <price>42.00</price>   </product> </xml> 

my script processes xml file this:

<?php $log = 'error log'.php_eol; $xml = simplexml_load_file('file.xml'); foreach($xml->'product' $key->$val) {   if(everything_is($okay)) {     //process product   }   else {     $log .= 'there error product on line '.$childline.php_eol;   } } file_put_contents('log.txt',$log); ?> 

now, thing need know here is, there way find line current child in foreach loop on in xml file?..

i know libxml_get_errors() reports line of xml-errors, if determine product has flaws, want find out line product appears on in xml file.

i thinking use $key of child ask simplexml found child or something, don't know how.

if have solution or suggestion, feel free share it.

that's not possible simplexml. but, if use normal php dom can use domnode::getlineno line number of dom node.

so, either drop simplexml , go full dom, or, parse document in full dom when there error , find product node (and it's line number) there.

edit: here's dom example. it's off top of head, untested, pretty identical yours:

$xml = new domdocument(); $xml->load('file.xml');  foreach ($xml->getelementsbytagname('product') $product) {     if (everything_is_okay($product)) {         //     } else {         $log .= 'bad product on line ' . $product->getlineno();     } }  file_put_contents('log.txt',$log); 

for more information, check php dom documentation


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -