1

I have the following XML-structure:

   <Folder>
      <Placemark>
        <ExtendedData>
          <Data name="Id">
            <value>152285415</value>
          </Data>
          <Data name="Name">
            <value>Tester</value>
          </Data>
        </ExtendedData>
      </Placemark>
    </Folder>

and I need to directly access the of the -Object with the attribute "name" = "Id".

So I tried this:

$xml->Document->Folder->Placemark->ExtendedData->xpath('data[@name="Id"]')

but that gives and empty array.

What I need in the end is "152285415"

Any help appreciated.

3
  • I think you missed a key word in your question. What output do you want to get? Commented Aug 30, 2017 at 22:21
  • @RaphaelJeger you should close this question by accepting an answer. Commented Mar 4, 2018 at 16:53
  • @ishegg you're right, just done! Thx Commented Mar 8, 2018 at 20:41

3 Answers 3

1

You can use XPath to access what you want directly:

<?php
$xml = '<Folder>
      <Placemark>
        <ExtendedData>
          <Data name="Id">
            <value>152285415</value>
          </Data>
          <Data name="Name">
            <value>Tester</value>
          </Data>
        </ExtendedData>
      </Placemark>
    </Folder>';

$xml = new SimpleXMLElement($xml);
$result = $xml->xpath('/Folder/Placemark/ExtendedData/Data[@name="Id"]');
echo "Value: ".$result[0]->value; // Value: 152285415

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

This seems to do the job:

<?php

$xml = '<Folder>
      <Placemark>
        <ExtendedData>
          <Data name="Id">
            <value>152285415</value>
          </Data>
          <Data name="Name">
            <value>Tester</value>
          </Data>
        </ExtendedData>
      </Placemark>
    </Folder>';

$xml = simplexml_load_string($xml);

foreach($xml->Placemark->ExtendedData->Data as $item) {

    if($item->attributes()['name'] == 'Id') {
        echo $item->value;
    }

}

2 Comments

sure, it does. However directly accessing that object without looping would be much nicer, don't you think?
It would. Hopefully somebody more talented than me comes up with a better answer and then I can learn from them!
0

To not worry about the overall hierarchy of the document (although this is abused quite a bit) is to use // as the first part of the XPath. So the query can be written as in...

<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$xmlDoc = <<<XML
<Folder>
  <Placemark>
    <ExtendedData>
      <Data name="Id">
        <value>152285415</value>
      </Data>
      <Data name="Name">
        <value>Tester</value>
      </Data>
    </ExtendedData>
  </Placemark>
</Folder>
XML;

$xml = new SimpleXMLElement($xmlDoc);
$value = $xml->xpath('//Data[@name="Id"]/value')[0];
echo "Value=".$value.PHP_EOL;

Although note that $value is still a SimpleXMLElement Object, so if you need it as a string, you should use (string)$value. In the example, echo will automatically do the conversion for you.

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.