xml - select whole node with its attribute value too with xpath -


hello have strange question (for me strange). have xml file below:

<?xml version="1.0" encoding="iso-8859-1"?> <studentslist> <student> <student_id email="test@yahoo.com">18700</student_id> <firstname>jhon</firstname> <lastname>smith</lastname> <address>dragon vally china</address> </student> <student> <student_id email="leesin@gmail.com">18701</student_id> <firstname>lee</firstname> <lastname>sin</lastname> <address>league of legend uk</address> </student> </studentslist> 

when search file using xpath in php

$allstudents = $xml->xpath("//studentslist/student"); 

it give me students in array child node , values below:

array ( [0] => simplexmlelement object ( [student_id] => 18700 [firstname] => jhon [lastname] => smith [address] => dragon vally china ) [1] => simplexmlelement object ( [student_id] => 18701 [firstname] => lee [lastname] => sin [address] => league of legend uk ) [2] => simplexmlelement object ( [student_id] => 18702 [firstname] => xin [lastname] => xaho [address] => shanghi china ) [3] => simplexmlelement object ( [student_id] => 18703 [firstname] => corki [lastname] => adc [address] => flying machine gun china ) [4] => simplexmlelement object ( [student_id] => 18704 [firstname] => kog [lastname] => maw [address] => depth of hell ) ) 

but have attribute student_id node want value of attribute can 1 please suggest google lot didn't find right answer. gave examples selecting parent attribute value atleast didn't find 1 select whole node of student.

you can access attribute named foo $element['foo'], e.g.

$xml = <<<xml     <studentslist> <student> <student_id email="test@yahoo.com">18700</student_id> <firstname>jhon</firstname> <lastname>smith</lastname> <address>dragon vally china</address> </student> <student> <student_id email="leesin@gmail.com">18701</student_id> <firstname>lee</firstname> <lastname>sin</lastname> <address>league of legend uk</address> </student> </studentslist> xml;  $doc = new simplexmlelement($xml);  foreach ($doc->student $student) {     echo $student->student_id['email'] . "\n"; } 

outputs 2 email addresses , of course xpath code same:

$xml = <<<xml     <studentslist> <student> <student_id email="test@yahoo.com">18700</student_id> <firstname>jhon</firstname> <lastname>smith</lastname> <address>dragon vally china</address> </student> <student> <student_id email="leesin@gmail.com">18701</student_id> <firstname>lee</firstname> <lastname>sin</lastname> <address>league of legend uk</address> </student> </studentslist> xml;  $doc = new simplexmlelement($xml);   foreach ($doc->xpath("//studentslist/student") $student) {     echo $student->student_id['email'] . "\n"; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -