Jump to content

[SOLVED] Problem loading xml document with DOMDocument->load


swoods

Recommended Posts

Hello,

 

I'm fairly new to this game and I hope I'm missing something simple, but I can't see it.

 

I'm trying to read an xml file using PHP's DOMDocument function and receiving this error:

 

Warning: DOMDocument::load() [function.DOMDocument-load]: Start tag expected, '<' not found in file....[file name here]

 

There are two php files involved.

 

The first grabs data from a mysql db and outputs it as xml. The page has the header(Content-Type = text/xml) at the top and the code generating the xml looks like this:

 

echo "<?xml version= '1.0' encoding= 'UTF-8'?>";
while($results = mysql_fetch_array($result)){
        echo "<getBlogposts>
                <blogpostResult>
                Blogposts retrieved successfully
                </blogpostResult>
                <blogpost>
                <blogAuthor>"
                .$results['postAuthor'].
                "</blogAuthor>
                <blogDate>"
                .$results['postDate'].
                "</blogDate>
                <blogTitle>"
               .$results['postTitle'].
               "</blogTitle>
               <blogContent>"
               .$results['postContent'].
               "</blogContent>
              </blogpost>";
       }
echo "</getBlogposts>";

 

That seems to generate a valid xml file, you can see it here:

http://www.streetswimming.co.uk/zw/get_posts_public.php

 

The second uses the DOMDocument method to load the php file, and then prints the content.

function parse_xml(){
    $xmlDoc = new DOMDocument();
    $xmlDoc->load("get_posts.php");

    $x = $xmlDoc->getElementsByTagName('blogpost');

    foreach ($x AS $item) {
          print $item->nodeName . " = " . $item->nodeValue . "<br />";
    }
}

And that results in the error above. You can see that here: http://www.streetswimming.co.uk/zw/parse_xml_public.php

 

If I copy the page source of get_posts_public.php and save it as an xml file and load that file directly, it works. If I replace 'get_posts.php' in the load function with another php page which generates xml (so I do something like  $xmlDoc->load("http://www.w3schools.com/php/responsexml.php?q=1"; ), it works too.

 

Can anyone tell me where I'm going wrong?

 

Many thanks,

 

Simon.

This line:

    $xmlDoc->load("get_posts.php");

will load the 'local file', aka the PHP code. If you want to load the generated version, you need to provide the URL:

    $xmlDoc->load("http://www.streetswimming.co.uk/zw/get_posts_public.php");

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.