如何从 PHP 输出 XML 字符串 [英] How to output XML string from PHP

查看:30
本文介绍了如何从 PHP 输出 XML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取一个 XML 字符串并在 PHP 中对其进行编辑,以便在访问 PHP 文件时最终输出编辑后的 ​​XML 字符串.我一直在尝试使用 echo 和 print 来输出 XML 文档,但它只是在最里面的标签中打印数据.我希望它能够像直接加载 XML 文档(例如 test.com/example.xml)一样运行.相反,它只是打印出字符串的一部分而不是整个内容.打印语句如下.有什么建议吗?

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?

打印<代码><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">

<代码><文档><Style id="undecorated_style">未修饰</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

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

而不是简单的 xml.

instead of simply the xml.

推荐答案

在打印之前,添加内容类型标题.

Before you print, add content type header.

要么

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

或更适合 KML

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

如果你想在浏览器中查看源代码,请阅读:http://www.w3schools.com/xml/xml_view.asp

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

您还可以通过添加

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

这篇关于如何从 PHP 输出 XML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆