0

This is my XML file

<?xml version="1.0" encoding="UTF-8"?>
<searchresult>
  <query>file</query>
  <page-number>3</page-number>
  <start>20</start>
  <files-per-page>10</files-per-page>
  <files-approx-count>6361998</files-approx-count>
  <result-files>
    <file>
      <name>file 1</name>
      <description>
        descrp
      </description>
      <url>
        http://www.example.com
      </url>
    </file>
    <file>
      <name>file 2</name>
      <description>
        descrp 2
      </description>
      <url>
        http://www.example.com
      </url>
    </file>
  </result-files>
</search-result>

i tried the following code

$xml = simplexml_load_file("file.xml");
foreach ($xml as $xmls):
    $name =$xmls->name;
    $url =$xmls->url;
    echo $name.$url;
endforeach;

but no output please help.

regilero
  • 29,806
  • 6
  • 60
  • 99
Rahul R
  • 209
  • 2
  • 11

2 Answers2

3

First thing, your XML file is not well-formed. The root tag is <searchresult> while the last tag is </search-result>. This causes a parsing error.

Second thing, if your tags contain dashes, those tags cannot be used directly as variables with SimpleXML, in that case you should use a special syntax (see this: php simplexml_load_file with a dash ( - )). Other way to fix this is, if you control de XML syntax, change the way the XML is written and don't use dashes on tags.

Lastly, I think you want to print out the info of files within the result-files tag.

$xml = simplexml_load_file("file.xml");

foreach ($xml->{'result-files'}->file as $file) {
    printFile($file);
}

function printFile($file) {
    $name = trim($file->name);
    $url = trim($file->url);
    print "$name $url\n";
}

Output:

file 1 http://www.example.com
file 2 http://www.example.com

And you are done.

Community
  • 1
  • 1
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • +1 nice answer but not sure what the benefit of the printFile function is. Also I would explicitly cast as a string `$name = (string)$file->name;` because if you do anything other than echo it, it will remain as a SimpleXML element object. – MrCode Jul 01 '13 at 15:42
  • No direct benefit :) I just wanted to make it more readable, like iterate through each file and do something with it. What to do? Print its name and url. – Diego Pino Jul 01 '13 at 17:19
  • Thanks.. it worked , But i am unable to edit the XML file because its the output of another API from 3rd party. – Rahul R Jul 02 '13 at 02:07
0

You're creating an object when you use the simplexml_load_file. The object isn't necessarily iterable - but it is indexable by tag names. try not doing the foreach(...) and instead just using echo $xml->name;. If that doesn't work, then perhaps you're looking in the wrong directory for your file. You likely need to do something like:

EDITED TO MATCH XML FILE PROVIDED

$path = $_SERVER['DOCUMENT_ROOT']."/file.xml";
$xml = simplexml_load_file($path);
echo $xml->start; //should output 20
RutledgePaulV
  • 2,568
  • 3
  • 24
  • 47
  • please run the above code with MY XML file ( Result is no error no o/p ) – Rahul R Jul 01 '13 at 15:18
  • I feel like you're just playing with someone else's code that you don't understand.. instead - learn and build your own code. Never grab and use code that doesn't make sense to you - or if you want to use it, learn how it works first. – RutledgePaulV Jul 01 '13 at 15:36