RNs_Ghost
RNs_Ghost

Reputation: 1777

Output raw XML using php

I want to output raw xml in a manner similar to http://www.google.com/ig/api?weather=Mountain+View but using PHP.

I have a very simple php script on my webserver:

<?php 

      $output = "<root><name>sample_name</name></root>";
      print ($output);
?> 

All I can see in Chrome/firefox is "sample_name". I want to see:

<root>
     <name>sample_name</name>
</root>

I cannot find a tutorial for anything THIS simple.

Thanks

Upvotes: 30

Views: 75682

Answers (6)

Meisam Rasouli
Meisam Rasouli

Reputation: 321

I suggest using the PHP's built-in functions designed for creating an XML file. These functions are:

xmlwriter_open_memory();
xmlwriter_output_memory();

xmlwriter_open_memory() assigns a place in the memory for creating an xml file. xmlwriter_output_memory() generates the final string which represents the contents of the xml file.

for creating a node with some text inside you have to use the following three functions in sequence.

xmlwriter_start_element($xDoc, $Name);
    xmlwriter_text($xDoc, $text);
xmlwriter_end_element($xDoc); 

The following piece of code shows you how to use these functions to generate the output shown below.

the result:

<?xml version="1.0" encoding="UTF-8"?>
<RootX attribute1="attribute1Value">
   <Node3>TextC</Node3>
   <Node6>
      <node4>node4 value</node4>
      <node5>node5 value</node5>
   </Node6>
</RootX>

the code:

 <?php

function createXmlElement(&$xDoc, $Name, $text)
{
    xmlwriter_start_element($xDoc, $Name);
    if ($text != null) {
        xmlwriter_text($xDoc, $text);
    }
    xmlwriter_end_element($xDoc); // $Name
}


header("Content-Type: text/xml; charset=UTF-8", true); // use for files
$xmlDoc = xmlwriter_open_memory();  // open a place in the memory for creating an xml document
xmlwriter_set_indent($xmlDoc, 1);
$res = xmlwriter_set_indent_string($xmlDoc, '   '); // sets the indentation characters
xmlwriter_start_document($xmlDoc, '1.0', 'UTF-8'); /* starts the xml document by writing the information node <?xml version="1.0" encoding="UTF-8"?> */
xmlwriter_start_element($xmlDoc, 'RootX'); // the first node named RootX
{
        xmlwriter_start_attribute($xmlDoc, 'attribute1'); // attribute opening
        xmlwriter_text($xmlDoc, 'attribute1Value'); // attribute's value
        xmlwriter_end_attribute($xmlDoc); // attribute closing
        {
            {
                createXmlElement($xmlDoc, 'Node3', 'TextC');
            }
            {
                xmlwriter_start_element($xmlDoc, 'Node6');
                {
                    createXmlElement($xmlDoc, 'node4', 'node4 value');  // generate a node which contains text
                    createXmlElement($xmlDoc, 'node5', 'node5 value');  // generate a node which contains text
                }
                xmlwriter_end_element($xmlDoc); // closing element
            }
        }
}
xmlwriter_end_element($xmlDoc); // closing element
echo xmlwriter_output_memory($xmlDoc);  // generates the result

The echo function at the end sends the results to the browser. $xmlDoc is the xml document object created by xmlwriter_open_memory().

Note that the xml document object is passed to function createXmlElement by reference.

createXmlElement(&$xDoc, $Name, $text)  //$xDoc is passed by reference, note the ampersand

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98861

<?php 
    header('Content-Type: application/xml');
    $output = "<root><name>sample_name</name></root>";
    print ($output);
?> 

Upvotes: 16

user110357
user110357

Reputation: 59

Just press Ctrl+U (Control+U), Your browser will display the page's raw source code under a new Tab, be it XML or HTML or whatever else...

Upvotes: 0

IMSoP
IMSoP

Reputation: 97638

For completeness sake, as nobody's mentioned it yet, you can also output raw XML within an HTML page, by escaping < as &lt;, > as &gt;, and & as &amp;. To do that, use the awkwardly named htmlspecialchars function.

Often, you'll also want to preserve whitespace in the argument, which can be done by surrounding it in <pre> tags. So a common line for debugging or including a sample is this:

 echo '<pre>' . htmlspecialchars($raw_xml) . '</pre>';

Upvotes: 5

Lukas Knuth
Lukas Knuth

Reputation: 25755

You only see "sample_name" because you didn't specify that your output is XML and the browser thinks it's HTML so your XML-Elements are interpreted as HTML-Elements.

To see the raw XML, you need to tell the browser that you're actually sending XML by sending the correct content-type in the HTTP header:

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

This needs to be send (put in the code) before you put out anything else.

Upvotes: 4

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99879

By default PHP sets the Content-Type to text/html, so the browsers are displaying your XML document as an HTML page.

For the browser to treat the document as XML you have to set the content-type:

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

Do this before printing anything in your script.

Upvotes: 70

Related Questions