简体   繁体   English

为什么我的所有XML都被转义了?

[英]Why is ALL my XML being escaped?

Long time listener, first time caller. 长时间的聆听者,首次呼叫者。

Here is what I got going on... 这就是我正在进行的事情...

Created a PHP webservice, no problem with sending simple data types like string, int... No problem returning string or int. 创建了PHP Web服务,发送简单的数据类型(如字符串,整数)没有问题...返回字符串或整数没有问题。

Now I want to return data in XML. 现在,我想以XML返回数据。

So I created a DomDocument and filled in all the data... 所以我创建了一个DomDocument并填充了所有数据...

Then I return it out of the service. 然后我将其退回服务范围。

On my client side when I go to load the object as an XML document I get an error about my root not being properly formated. 在我的客户端上,当我将对象作为XML文档加载时,出现有关我的root格式不正确的错误。

I look at my debug and all the "special" characters have been escaped. 我查看调试信息,所有“特殊”字符均已转义。

I did a hack and replaced "<" with > and so on. 我做了一个hack,然后用“>”替换了“ <”,依此类推。

Then I can load the result. 然后,我可以加载结果。

This does not seem right, I should be able to return XML with it properly formatted. 这似乎不对,我应该能够以正确的格式返回XML。

So I am guessing it is in how I defined the xml object??? 所以我猜这是我如何定义xml对象?

Can someone post a sample of PHP defining the return of an xml object? 有人可以发布一个定义XML对象返回的PHP示例吗?

I have looked for days and I cannot find a simple example of selecting data from MySql turning it to XML and then returning the XML to a client. 我花了好几天的时间,但找不到一个简单的示例,即从MySql中选择数据并将其转换为XML,然后将XML返回给客户端。

I did a hack and replaced "<" with > and so on.

There's no hack about replacing .XML harmful characters , its the rule. 替换.XML有害字符没有规则,这是规则。 Read here about such characters. 在这里阅读有关此类字符的信息。 Instead, you can use this code: 相反,您可以使用以下代码:

function replace_characters_for_xml(&$str)
{
   $str = str_replace("&","&amp;",$str);
   $str = str_replace(">","&gt;",$str);
   $str = str_replace("<","&lt;",$str);
   $str = str_replace("'","&apos;",$str);
   $str = str_replace('"',"&quot;",$str);
}
...
replace_characters_for_xml($string);

So I created a DomDocument and filled in all the data... 所以我创建了一个DomDocument并填充了所有数据...

I suspect that whatever you did here was done wrong. 我怀疑您在这里所做的任何操作都是错误的。

I found a different code example for storing MySql data into XML. 我找到了一个不同的代码示例,用于将MySql数据存储到XML中。 This did the trick as to what I was looking for as a result. 这确实使我得到了想要的结果。

Using PHP DOM to create XML files from MySQL data 使用PHP DOM从MySQL数据创建XML文件

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

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