user715234
user715234

Reputation:

How to output XML string from PHP

I'm taking an XML string and editing it in PHP to finally output the edited XML string when the PHP file is accessed. I've been trying both echo and print to output the XML document, but it's only printing the data within the innermost tags. I want this to function as if you loaded an XML document directly such as test.com/example.xml. Instead it's only printing out part of the string instead of the whole thing. The print statement is below. Any advice?

print <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

<Document> <Style id="undecorated_style"> <BalloonStyle> <displayMode>undecorated</displayMode> </BalloonStyle> </Style>

<Placemark>
    <name>Relative Marker Example</name>
    <description><![CDATA[
    <div style="position: absolute; width: 100px; height: 100px; left: -50px; top: -50px"><img width="100px" height="100px" src="http://argonapps.gatech.edu/examples/FrameMarkerThin_005.png"/></div>
    ]]>
    </description>
    <balloonVisibility>1</balloonVisibility>
    <Marker>
        <markerType>framesimpleid</markerType>
        <markerId>-1</markerId> <!-- integer value (-1 means follow any marker of markerType) -->
        <locationMode>relative</locationMode> <!-- default (ignore), relative (update location), fixed (update camera) -->
        <orientationMode>fixed</orientationMode>
        <scale>
            <x>0.076</x> <!-- test marker is 0.038 meters -->
            <y>0.076</y>
            <z>0.076</z>
        </scale>
    </Marker>
    <styleUrl>#undecorated_style</styleUrl>
</Placemark>

';

Opening the file in a browser shows only:

Relative Marker Example ]]> 1 framesimpleid -1 relative fixed 0.076 0.076 0.076 #undecorated_style

instead of simply the xml.

Upvotes: 2

Views: 15091

Answers (3)

vartec
vartec

Reputation: 134601

Before you print, add content type header.

Either

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

or more proper for KML

header('Content-type: application/vnd.google-earth.kml+xml');

If you want to view it in the browser as source, read this: http://www.w3schools.com/xml/xml_view.asp

You can also force it to be displayed as text, by adding

 header('Content-type: text/plain');

Upvotes: 5

fin1te
fin1te

Reputation: 4351

It's probably because your browser is trying to render the XML (similar to how it loads HTML, you just see the content not the tags).

If you do "View Source" in your browser, you should see the raw XML.

You could always change the tags to &lt; and &gt; so that it shows it as text

print htmlentities('<Placemark>.....</Placemark>');

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270617

Before echoing your output, you should first send the correct header for XML data:

header("Content-type: text/xml");
// Or
header("Content-type: application/xml");

EDIT: Note that your browser will probably still render only the inner text. View the page source to see your XML.

Upvotes: 0

Related Questions