Jump to content

Grabbing Dynamically Generated XML


DVigneault

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/266429-grabbing-dynamically-generated-xml/
Share on other sites

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;

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...

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?

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.

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.