2

I am using the XPath in PHP 5 to parse a XML document. The problem I have is writing a foreach to correctly display the following array:

XML document sample

value 1 value 2

           $xmlfile = 'link_to_file.xml';                       
            $xmlRaw = file_get_contents($xmlfile);      
            $xml = new SimpleXMLElement($xmlRaw);
            $install_files = $xml->xpath('//files');
            foreach($install_files as $row)
            {
              echo $row['file'];
            }

//var_dump for $row gives the following

               array(1) {
                  [0]=>
                  object(SimpleXMLElement)#21 (2) {
                    ["file"]=>
                    string(12) "value 1"
                    ["folder"]=>
                    string(8) "value 2"
                  }
                }

Ideally I would like to get the value by using $row['file'] or $row['folder']. Thanks for any help.

4
  • 1
    @mjames, is this homework related? Commented Apr 17, 2010 at 20:41
  • It is actually for a script I am creating. Commented Apr 17, 2010 at 20:52
  • cool... just checking, the title seemed a little "homework-ish". Commented Apr 17, 2010 at 20:59
  • I actually had a different title but it was changed by the moderators :) Commented Apr 17, 2010 at 21:06

3 Answers 3

2

The items in your array are SimpleXMLElement objects. You can access their properties with the $object->property syntax. So try this:

foreach ($array as $row) {
    echo $row->folder . $row->file;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did not realize i had to use it in an object form ->file instead of ['file]. Works great now :)
0

If I understand your example correctly,

foreach ($array as $row)
{
    // do something with $row['file'] or $row['folder']
}

Comments

0

If you want to process only one element you don't necessarily need a loop, but it doesn't hurt either.
According to your output file and folder are child-elements of the element returned for your xpath query. You can access them via $parentElement->childElement. If they were attributes you'd use $element['attributeName']

$foo = new SimpleXMLElement(getData());

foreach( $foo->xpath('row[@id="r2"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}
echo "-----\n";

// same thing if you have more than one element in your result set
foreach( $foo->xpath('row[@id="r2" or @id="r3"]') as $row ) {
  echo $row->file, " | ", $row->folder, "\n";
}

function getData() {
  return '<foo>
    <row id="r1">
      <file>value 1.1</file>
      <folder>value 1.2</folder>
    </row>
    <row id="r2">
      <file>value 2.1</file>
      <folder>value 2.2</folder>
    </row>
    <row id="r3">
      <file>value 3.1</file>
      <folder>value 3.2</folder>
    </row>
  </foo>';
}

prints

value 2.1 | value 2.2
-----
value 2.1 | value 2.2
value 3.1 | value 3.2

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.