1

I have an XML but I can't seem to access any of the nodes.

When I use the code below I can see it is picking up the data but I can't seem to access the nodes individually.

$xml=simplexml_load_file("uploads/" . $fileName) or die("Error: can't create     object");
echo'<pre>';
print_r($xml);
echo'</pre>';

Here is a sample record.

<CONSOLIDATED_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<INDIVIDUALS>
<INDIVIDUAL   xsi:noNamespaceSchemaLocation="">
<DATAID>110404</DATAID>
<VERSIONNUM>1</VERSIONNUM>
<FIRST_NAME>MOHAMMAD BAQER</FIRST_NAME>
<SECOND_NAME>ZOLQADR</SECOND_NAME>
<UN_LIST_TYPE>Iran</UN_LIST_TYPE>
<REFERENCE_NUMBER>IRi.043</REFERENCE_NUMBER>
<LISTED_ON>2007-03-24</LISTED_ON>
<COMMENTS1>[Old Reference # I.47.D.7]</COMMENTS1>
<DESIGNATION>
<VALUE>General/IRGC officer</VALUE>
<VALUE>Deputy Interior Minister for Security Affairs</VALUE>
</DESIGNATION>
<LIST_TYPE>
<VALUE>UN List</VALUE>
</LIST_TYPE>
<LAST_DAY_UPDATED>
<VALUE>2014-12-17</VALUE>
</LAST_DAY_UPDATED>
<INDIVIDUAL_ALIAS>
<QUALITY>Good</QUALITY>
<ALIAS_NAME>
Mohammad Bakr Zolqadr; Mohammad Bakr Zolkadr; Mohammad Baqer Zolqadir;     Mohammad Baqer Zolqader
</ALIAS_NAME>
</INDIVIDUAL_ALIAS>
<INDIVIDUAL_ADDRESS/>
<INDIVIDUAL_DATE_OF_BIRTH/>
<INDIVIDUAL_PLACE_OF_BIRTH/>
<INDIVIDUAL_DOCUMENT/>
<SORT_KEY>Zolqadr</SORT_KEY>
<SORT_KEY_LAST_MOD>2014-06-18</SORT_KEY_LAST_MOD>
</INDIVIDUAL>

I need to access each of the values, blank or otherwise and put them into variables.

I've been trying all kinds of code here is a very basic example, but I can't seem to access the data

 foreach($xml->children() as $child)
 {
 echo $child->individuals->individual->dataid;
 }
1
  • You can easily access this data via :- $xml->individuals->individual[0]->nodename; Commented Oct 19, 2015 at 15:03

1 Answer 1

2

You can loop over the INDIVIDUALS array and access member objects like this

foreach($xml->INDIVIDUALS->INDIVIDUAL as $node){
    echo $node->DATAID . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.