使用PHP xml解析器处理符号


Handling Symbols with PHP xml parser

我正在用PHP的xml解析器编写一个xml到文本文件的脚本。我用@符号分隔属性,用|符号分隔数据,当我打开文本文件时,我注意到符号被视为它们自己的数据。

i.e. <equation valid="yes">For this theorem assume X < Y and Z & A = 0</equation>
should have output @yes@ |For this theorem assume X < Y and Z & A = 0|
but instead I have @yes@ |For this theorem assume X| |<| |Y| and Z | |&| | A = 0|

所以我相信正在发生的是,任何依赖&[a-z]+;或者&[0-9]+;被视为两个标签之间的不同数据。我尝试了preg_replace("/&([a-z]+);/","'1",$data)和同样的数字,只是在那里有字母/数字供我稍后格式化,但这似乎没有任何作用。如何让xml解析器将这些符号识别为数据的一部分?

第1版:我刚才也试着把所有&带有''''amp;但这也没什么作用,我只是在数据分隔符之前加了反斜杠。

我相信你在问字符串中的特殊字符?如果是这样,您需要使用ascii等价物,即

    For this theorem assume X < Y and Z & A = 0
    Becomes
    For this theorem assume X &lt; Y and Z &amp; A &#61; 0