0

I try to parse an XML file, the code I have only works when I add the XML directly in the PHP code, but I need to get it working with an external file.

I tried many ways to get it working, but without success.

UPD. No advises were helpful, I found solution by myself.

Thank you for all who try to help me.

<?php

  $url = "link to file";
  $domSrc = file_get_contents($url);
  $dom = new DomDocument();
  $dom->loadXML( $domSrc );

        while(is_object($finance = $dom->getElementsByTagName("item")->item($i)))
        {
                 foreach($finance->childNodes as $nodename)
                 {
                        if($nodename->nodeName=='id')
                            {
                            echo "<p>$nodename->nodeValue</p>";
                            }
            }
        }
        $i++;
    }
?>
  • Please define *external file*. What does external mean here? Also please provide an XML sample from that file. Also enable error reporting and logging, that should give you more hints, see: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Mar 03 '14 at 15:56

2 Answers2

0

The DOMDocument has several methods to load contents. In your case DOMDocument::load() should be the right choice. Just replace:

$doc->loadXML($xml);

with

$doc->load($xml);

The method has full support for the PHP streamwrappers. But is a good idea to always use an absolute path and not rely on the current working directory.

$xml = __DIR__.'/move.xml';
ThW
  • 19,120
  • 3
  • 22
  • 44
-1
$xml = simplexml_load_file('giveYourUrl');

try it

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38