3

I am trying to parse an XML file using PHP in order to find an attribute value.

The XML appears as follows(example.xml):

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<xryfile XRYVersion="6.11.1.0">
<images>
<image type="PHYSICAL" id="0" version="6.11.1">
  <views>
    <view type="location_history_view">
      <nodes>
        <node>
          <properties>
            <property type="application">Facebook Messenger</property>
            <property type="longitude">-1.000000</property>
            <property type="latitude">1.000000</property>
          </properties>
        </node>
      </nodes>
    </view>
  </views>
</image>
</images>
</xryfile>

I wish to extract the values of the "property" nodes but I am unsure of the syntax to use to extract this information.

I have tried the following code:

<?php

$xml = simplexml_load_file('example.xml');



foreach($xml->property[0]->attributes() as $a) {
echo $a;
}
?>

I can work out how to extract a node easily if the node was say:

<property>Facebook Messenger</property>

However I am having trouble working out how to extract attribute values when it appears like this:

<property type="application">Facebook Messenger</property>

I would very much appreciate some help with the problem as I have been stuck for a while now.

Thanks in advance :)

3 Answers 3

3

Is there a particular reason you must use SimpleXML? if none, you could try with DOMDocument instead. Code is below:

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->load('example.xml');

$properties = $dom->getElementsByTagName('property');
foreach($properties AS $property)
{
    $value = $property->nodeValue;
    $type = $property->getAttribute('type');
    echo '<div>Node Information/Value :'. $value. '<br/>'. 'Node attribute:'. $type. '</div>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Appreciate your response. Works perfectly for attributes. However If I wanted to extract the node value (ie "Facebook Messenger") could I use: $type = $property->getAttribute('type=[application]'); ?
Nope. You'd use: $property->nodeValue; And if this solves your problem, try to mark it as the accepted answer, so that others can benefit from it when faced with similar issues.
Apologies. I may have been unclear with my question. I wish to extract the information for example Facebook Messenger. from the node: <property type="application">Facebook Messenger</property>As oppose to the "application" value
I've updated my answer to take that into account. Hope that helps.
If you're using DOM, read about DOMXPath::evaluate(). It allows you to use XPath expressions to extract nodes and values.
2

Try this:

foreach($xml->property AS $property) {
   if(isset($property['type'])){
     $a = $property['type'];
     echo $a;
   }
}

1 Comment

Thanks for the reply, It is not returning any errors which is a plus, but also not echoing the values.
2

This echoes all 3 attributes:

foreach($xml->images->image->views->view->nodes->node->properties->property as $obj){
    echo $obj->attributes()->type."<br/>";
}

Result:

application
longitude
latitude

2 Comments

Never knew that @n-dru. So, plus 1 for you. Although, I must note that the chain is kinda long. Reason I prefer the DOM approach which gives direct access, rather than sequential access.
Thanks, I appreciate it. It is just one of the ways, fact is that it is only for this specific structure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.