How to get the value of a specific nested XML node in PHP? -


i need value of every "custom_field" named "zip" out of following xml-file using php. when parse it, either values of projects or empty array? can help?

<?xml version="1.0" encoding="utf-8"?> <projects total_count="237" offset="0" limit="100" type="array"> <project>     <id>239</id>     <name>abc</name>     <identifier></identifier>     <description></description>     <status>1</status>     <is_public>false</is_public>     <custom_fields type="array">         <custom_field id="18" name="name affix">             <value></value>         </custom_field>         <custom_field id="20" name="zip">             <value>x1111</value>         </custom_field>     </custom_fields>     <created_on>2017-06-05t16:33:13z</created_on>     <updated_on>2017-06-19t13:46:08z</updated_on> </project> <project>     <id>240</id>     <name>def</name>     <identifier></identifier>     <description></description>     <status>1</status>     <is_public>false</is_public>     <custom_fields type="array">         <custom_field id="18" name="name affix">             <value></value>         </custom_field>         <custom_field id="20" name="zip">             <value>y2222</value>         </custom_field>     </custom_fields>     <created_on>2017-06-05t16:33:14z</created_on>     <updated_on>2017-06-05t16:33:14z</updated_on> </project> ... 

i tried following , empty arrays:

$projects = simplexml_load_file($rm_host."/projects.xml?key=".$rm_sa_key."&limit=100");  foreach($projects->project $project){      $zip = $project->xpath('//custom_field[@name="zip"]');      print_r($zip);     echo "<br/>"; } 

when try replace string following, returns value of items, not of specific one:

zip = $project->xpath('//custom_fields[@type="array"]/custom_field[@name="zip"]') 

finally worked after trying , trying.

the correct string specific value was:

$zip = $project->custom_fields->xpath('custom_field[@name="zip"]/value')[0]; 

without slash in front of xpath.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -