DVigneault Posted July 29, 2012 Share Posted July 29, 2012 Hey all-- So I've got a php script that generates an xml page, which is working, as follows: <?php header ("Content-Type:text/xml"); echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> //Functions to generate xml I'm then trying to grab some of that data in another php script. In the past, when the xml was hard-coded (file extension .xml), I've used the following: $xml = new SimpleXMLElement(file_get_contents(/path/to/file)); When I try to use this to grab my dynamically-generated xml page above (file extension .php), I get a bunch of SimpleXMLElement::__construct() warnings. Sorry in advance if this is a naive question--I'm sure what I'm doing wrong will be glaringly obvious to the people reading this forum. Best, --Davis Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/ Share on other sites More sharing options...
requinix Posted July 29, 2012 Share Posted July 29, 2012 Put the logic that generates the XML as a string in some common place. Then use that in both scripts: the one script calls header() and outputs the XML, the other uses it for whatever. Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365316 Share on other sites More sharing options...
DVigneault Posted July 30, 2012 Author Share Posted July 30, 2012 Thanks! So I think understand how to store the hard-coded part of my XML as a string... <?php $xmlstr = <<<XML //Some hard-coded XML XML; ?> ...but what about XML I'm generating from a database? So far I've just been getting it from a database and echoing it like this: $con = new mysqli($host, $username, $password, $database); $result = $con->query("SELECT stuff"); mysqli_close($con); $row = mysqli_fetch_array($result); while ($row = mysqli_fetch_array($result)) { echo '<parent>'; echo '<child1>' . $row['something'] . '</child1>' . PHP_EOL; echo '<child2>' . $row['somethingelse'] . '</child2>' . PHP_EOL; echo '</parent>' . PHP_EOL; Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365321 Share on other sites More sharing options...
requinix Posted July 30, 2012 Share Posted July 30, 2012 Put it all into a variable instead of echoing it. Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365334 Share on other sites More sharing options...
DVigneault Posted July 30, 2012 Author Share Posted July 30, 2012 So I've got it outputting XML using ob_start(), ob_get_contents, and ob_end_clean(), as follows: $xml1 = <<<XML <urlset> //and some hard-coded XML XML; ob_start() $con = new mysqli($host, $username, $password, $database); $result = $con->query("SELECT stuff"); mysqli_close($con); $row = mysqli_fetch_array($result); while ($row = mysqli_fetch_array($result)) { echo '<parent>'; echo '<child1>' . $row['something'] . '</child1>' . PHP_EOL; echo '<child2>' . $row['somethingelse'] . '</child2>' . PHP_EOL; echo '</parent>' . PHP_EOL; $xml2 = ob_get_contents() ob_end_clean() $xml3 = '</urlset>'; $sitemap = $xml1 . $xml2 . $xml3; Printing $sitemap with the xml header works, but... $sitemapXML = new SimpleXMLElement($sitemap); foreach ($sitemapXML->path->to->element as $var) { echo $var->whatiwant; } ...gives me an error, saying that I'm supplying an invalid argument. Thanks for bearing with me... Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365350 Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 First and foremost, you should have done what requinix said in his/her last post. All that output buffering and multiple variable use is completely unnecessary. $sitemap = <<<XML <urlset> //and some hard-coded XML XML; $con = new mysqli($host, $username, $password, $database); $result = $con->query("SELECT stuff"); $row = mysqli_fetch_array($result); while ($row = mysqli_fetch_array($result)) { $sitemap .= '<parent>'; $sitemap .= '<child1>' . $row['something'] . '</child1>' . PHP_EOL; $sitemap .= '<child2>' . $row['somethingelse'] . '</child2>' . PHP_EOL; $sitemap .= '</parent>' . PHP_EOL; } $sitemap .= '</urlset>'; The less code you use to accomplish your goal, (generally) the easier it'll be to understand and maintain it. As for your error: What, exactly, is the error message and what line of your code triggers it? Also, how does your XML look like when completely generated? Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365388 Share on other sites More sharing options...
DVigneault Posted July 30, 2012 Author Share Posted July 30, 2012 Thanks for the help, ChristianF--I didn't realize that you could append to a variable like that. The output looks like: <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://example.com</loc> <lastmod>2012-07-30</lastmod> <changefreq>monthly</changefreq> <priority>0.5</priority> </url> //More <url></url> elements </urlset> In the code-block I gave in my last post, the error is on the foreach statement: foreach ($sitemapXML->path->to->element as $var) { and it says that I'm supplying an invalid argument. Warning: Invalid argument supplied for foreach() in path/to/what/i/just/wrote on line 5. Searching on google says that this could mean that I'm not supplying it with an array (and, infact, I just found the is_array() function, and is_array($sitemapXML) returns false). Thanks again for your help, everyone. Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365394 Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 I'd recommend running a var_dump ($sitemapXML) just before that foreach loop, to see what kind of data's in that variable. Should help you track down the real issue. You're welcome, by the way. Glad we could be of assistance. Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365400 Share on other sites More sharing options...
DVigneault Posted July 30, 2012 Author Share Posted July 30, 2012 var_dump() helped alot--I've got it working! The problem, it seems, was that I included the <urlset> parent element in my foreach path. When I got rid of that, everything worked like a charm. :-) Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365417 Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 Good to hear that you got it working, and happy to be of assistance. Thanks for sharing the solution to the problem as well. Quote Link to comment https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/#findComment-1365493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.