swoods Posted May 1, 2008 Share Posted May 1, 2008 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. Link to comment https://forums.phpfreaks.com/topic/103693-solved-problem-loading-xml-document-with-domdocument-load/ Share on other sites More sharing options...
rhodesa Posted May 1, 2008 Share Posted May 1, 2008 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"); Link to comment https://forums.phpfreaks.com/topic/103693-solved-problem-loading-xml-document-with-domdocument-load/#findComment-530971 Share on other sites More sharing options...
swoods Posted May 1, 2008 Author Share Posted May 1, 2008 Thank you so much, it works fine now. Link to comment https://forums.phpfreaks.com/topic/103693-solved-problem-loading-xml-document-with-domdocument-load/#findComment-530975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.