i have piece of xml follows
<records count="2"> <record> <firstname>firstname</firstname> <middlename>middlename</middlename> <lastname>lastname</lastname> <namesuffix/> <address> <street-number>demo</street-number> <street-pre-direction/> <street-name>demo</street-name> <street-post-direction/> <street-suffix>demo</street-suffix> <city>demo</city> <state>ny</state> <zip>demo</zip> <zip4>demo</zip4> <county>demo</county> </address> <phonenumberdetails> <phonenumber>demo</phonenumber> <listed>demo</listed> <firstname>demo</firstname> </phonenumberdetails> <dob day="" month="" year=""/> <age/> <date-first month="10" year="1999"/> <date-last month="04" year="2011"/> </record> <record> <firstname>firstname</firstname> <middlename>middlename</middlename> <lastname>lastname</lastname> <namesuffix/> <address> <street-number>demo</street-number> <street-pre-direction/> <street-name>demo</street-name> <street-post-direction/> <street-suffix>demo</street-suffix> <city>demo</city> <state>ny</state> <zip>demo</zip> <zip4>demo</zip4> <county>demo</county> </address> <phonenumberdetails> <phonenumber>demo</phonenumber> <listed>demo</listed> <firstname>demo</firstname> </phonenumberdetails> <dob day="" month="" year=""/> <age/> <date-first month="10" year="1999"/> <date-last month="04" year="2011"/> </record> </records> now, have been able data in php using simplexml except date-first , date-last elements. have been using code listed below
$datefirst = 'date-first'; $datelast = 'date-last'; $streetnumber = 'street-number'; $streetpredirection = 'street-pre-direction'; $streetname = 'street-name'; $streetpostdirection = 'street-post-direction'; $streetsuffix = 'street-suffix'; $unitdesignation = 'unit-designation'; $unitnumber = 'unit-number'; foreach ($reportdataxmlrecords->records->record $currentrecord) { echo $currentrecord->$datefirst['month'].'/'.$currentrecord->$datefirst['year']; echo $currentrecord->$datelast['month'].'/'.$currentrecord->$datelast['year']; echo $currentrecord->address->$streetnumber; $currentrecord->address->$streetname; // ......and on } where $reportdataxmlrecords part of simplexml object parent node of
but first 2 echo's don't print , other printing correctly, specifically, cant access data in
<date-first month="10" year="1999"/> <date-last month="04" year="2011"/> also debugging if do
print_r($currentrecord->$datefirst); it prints
simplexmlelement object ( [@attributes] => array ( [month] => 10 [year] => 1999 ) ) any appreciated. thank you.
you problem when do
$currentrecord->$datefirst['month'] php first evaluate $datefirst['month'] whole before trying use property
$datefirst = 'date-first'; var_dump( $datefirst['month'] ); // gives "d" because strings can accessed offset array notation, non-integer offsets converted integer , because casting 'month' integer 0, trying $currentrecord->d:
$xml = <<< xml <record> <date-first month="jan"/> <d>foo</d> </record> xml; $record = simplexml_load_string($xml); $var = 'date-first'; echo $record->$var['month']; // foo you can access hyphenated properties curly braces:
$record->{'date-first'}['month'] // jan on sidenote, when xml shown in question xml loading simplexml, e.g. when <records> root node, doing
$reportdataxmlrecords->records->record cannot work, because $reportdataxmlrecords root node , you'd have omit ->records if want iterate on record elements in it.
Comments
Post a Comment