简体   繁体   English

如何从PHP输出XML字符串

[英]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. 我正在使用XML字符串并在PHP中对其进行编辑,以便在访问PHP文件时最终输出已编辑的XML字符串。 I've been trying both echo and print to output the XML document, but it's only printing the data within the innermost tags. 我一直在尝试echo和print来输出XML文档,但是它只是在最里面的标签中打印数据。 I want this to function as if you loaded an XML document directly such as test.com/example.xml. 我希望它能像您直接加载XML文档(例如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"> 打印<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> <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 相对标记示例]]> 1个框架,-1相对固定0.076 0.076 0.076 #undecorated_style

instead of simply the xml. 而不是简单的xml。

Before you print, add content type header. 在打印之前,请添加内容类型标题。

Either

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

or more proper for KML 或更适合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 如果要在浏览器中作为源查看它,请阅读: 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');

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). 可能是因为您的浏览器试图呈现XML(类似于它加载HTML的方式,您只看到内容而不是标签)。

If you do "View Source" in your browser, you should see the raw XML. 如果在浏览器中执行“查看源代码”,则应看到原始XML。

You could always change the tags to &lt; 您可以随时将标签更改为&lt; and &gt; 和&gt; so that it shows it as text 以便将其显示为文本

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

Before echo ing your output, you should first send the correct header for XML data: echo输出之前,您应该首先发送正确的XML数据头:

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. 查看页面源以查看您的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM