Hi all,

Im working on this project, and am wanting to make up a temporary XML file but store it as a string in a PHP variable.

Im havign trouble making the string as i want it.

Heres my code which pulls the data from the database, and attempts to put it into XML tags. but when i echo the variable, it just gives me the PHP variables, and not the XML tags.

<?php
include('check_login.php');

$query = "SELECT * FROM stories WHERE approved='yes' ORDER BY date DESC";
$run = mysql_query($query);


$xml = '<?xml version="1.0" ?>';

	while($result = mysql_fetch_array($run))
	{
	$category = $result['category_id'];
	$headline = $result['headline'];
	$para1 = $result['para1'];
	$para2 = $result['para2'];
	$body = $result['body'];
	$journalist = $result['journalist'];
	$jpgurl = $result['jpgurl'];
	$wbmpurl = $result['wbmpurl'];
	$photographer = $result['photographer'];
	$date = $result['date'];

	$xml .= '<story>';
	$xml .= '<category>'.$category.'</category>';
	$xml .= '<headline>'.$headline.'</headline>';
	$xml .= '<para1>'.$para1.'</para1>';
	$xml .= '<para2>'.$para2.'</para2>';
	$xml .= '<body>'.$body.'</body>';
	$xml .= '<journalist>'.$journalist.'</journalist>';
	$xml .= '<jpgurl>'.$jpgurl.'"</jpgurl>';
	$xml .= '<wbmpurl>'.$wbmpurl.'</wbmpurl>';
	$xml .= '<photographer>'.$photographer.'</photographer>';
	$xml .= '<date>'.$date.'</date>';
	$xml .= '</story>';
	}

echo $xml;

?>

Please can anyone help me?

    what do you see when you view the source in your browser?

      oh!

      in source it shows it with the nodes there.

      Hmmm so how come this isnt showing as that in my main browser window?

      Thanks for that by the way, given me some hope

        thought it might. this isn't a php issue then. maybe a setting in your browser?

          Ah no idea about my browser it does the same on IE and Firefox.

          ANyway, so should this data in my string now work when i apply an XSLT to it?

            I'm no expert on XML at all, but I figure that you must save your $xml string in a file and save it as a XML in order for it to work as one. That includes the XSLT and anything else that can be done with XML file...

            The other idea:
            You might need to do something with Headers to make it output this file as a XML because this way it works as a plain HTML file (like it is with all PHP pages output to browser), but I'm no help on that either and this is just a suggestion from a brainstorm of mine...

              Hey Elliot,

              Just use:

              header('Content-Type: text/xml');

              and remove the:
              $xml = '<?xml version="1.0" ?>';

              Otherwise the browser will still think it's page type is txt/html. If you take a look at the page properties you'll see this.

              Greetz Ritter63

                Thanks for the replies....

                i added the header thing as u suggested, but now get an error....

                In IE its..

                The XML page cannot be displayed
                Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


                Only one top level element is allowed in an XML document. Error processing resource 'http://www.passecdl.co.uk/mpa/xml.php'...

                And in Firefox its....

                XML Parsing Error: junk after document element
                Location: http://www.passecdl.co.uk/mpa/xml.php
                Line Number 1, Column 309:

                Its getting there but slowly 😃

                  That's another problem alltogether.

                  You now have:
                  <Story>
                  <></>
                  <></>
                  <></>
                  </Story>
                  <Story>
                  <></>
                  <></>
                  <></>
                  </Story>

                  in your page... fill in the blanks with information you have in the page.

                  The problem is that you now have two story nodes that do NOT have a parent node.

                  If you put a parent node around all story nodes it will work. For instance:

                  <Stories>
                  <story>
                  <></>
                  <></>
                  <></>
                  </story>
                  <story>
                  <></>
                  <></>
                  <></>
                  </story>
                  <Stories>

                  Now that would work.

                  In practice this means that your code should be changed a little. You could for example place:
                  echo '<Stories>';
                  before entering the while loop.
                  and then put
                  echo '</Stories>';
                  just after the while loop.

                  The idea behind this is that in XML you should always have ONE parent node which is called a root node that ALL other nodes are children of. The only exceptions to this are nodes such as the nodes used for namespaces, stylesheets, encoding and such. But don't worry about that. You should remember that all you own data should ALWAYS be a child element of a root node. In this case the root node would be <Stories>

                  BTW in the other thread on the same subject I added that you should probably use both the header function and the:
                  $xml = '<?xml version="1.0" ?>';
                  statement to make sure you can export the page at a later date to some other application or some such.

                  Hope that helps.

                    Thanks very much, you have probably saved my life 😃

                    This is my last assignment of my degree, and we have had little time and tuition on this subject, as we have just had our big dissertations to hand in....

                    Also im helping others through this too, cos some of them arent too hot with php, so thanks for your help, you have saved us all.

                    Elliott
                    😃

                      Glad to help. Noticed you removed the

                      <?xml version='1.0'>

                      from $xml. Don't forget that it might be needed if you export the source page.

                      cya

                        Damn i guess i shouldnt have made it resolved, as ive hit another hitch.

                        I removed that line, and it gave me the proper xml tree structure, so im hoping that page is fine now.

                        The part im struggling with now, is to apply an XSL stylesheet to this $xml string.

                        ive been working with the example from w3schools, but its not working for me 🙁

                        Im gettin really confused, as ive had no prior experience i dont know what to do next. ill show you what i have.....this is my file that trys to apply them together.

                        and if u go Here u can see the result i get

                        the xml page is Here

                        the xsl page is Here

                        <?php
                        include('check_login.php');
                        include('xml.php');
                        
                        // $xml and $xsl contain the XML and XSL data
                        $arguments = array('/_xml' => $xml);
                        
                        // Allocate a new XSLT processor
                        $xh = xslt_create();
                        
                        // Process the document
                        $result = xslt_process($xh, 'arg:/_xml', 'net.xsl', NULL, $arguments);
                        
                        if ($result) {
                           echo "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
                           echo " variable, the \$result variable has the following contents\n<br />\n";
                           echo "<pre>\n";
                           echo $result;
                           echo "</pre>\n";
                        } else {
                           echo "Sorry, sample.xml could not be transformed by sample.xsl into";
                           echo "  the \$result variable the reason is that " . xslt_error($xh);
                           echo " and the error code is " . xslt_errno($xh);
                        }
                        xslt_free($xh);
                        ?> 
                        
                          Write a Reply...