Home  >  Article  >  Backend Development  >  Code sharing for xml parsing methods

Code sharing for xml parsing methods

黄舟
黄舟Original
2017-03-29 15:51:021612browse

XML文档中的所有文本都会被解析器解析。只有在CDATA部件之内的文本会被解析器忽略。

--------------------------------------------------------------------------------

解析数据
XML 解析器通常情况下会处理XML文档中的所有文本。

当XML元素被解析的时候,XML元素内部的文本也会被解析:

<message>This text is also parsed</message>

XML解析器这样做的原因是XML元素内部可能还包含了别的元素,象下面的例子,name元素内部包含了first和last两个元素:

<name><first>Bill</first><last>Gates</last></name>

解析器会认为上面的代码是这样的:

<name>
<first>Bill</first>
<last>Gates</last>
</name>

--------------------------------------------------------------------------------

转义字符
不合法的XML字符必须被替换为相应的实体。

如果在XML文档中使用类似"

<message>if salary < 1000 then</message>

为了避免出现这种情况,必须将字符"

<message>if salary < 1000 then</message>

下面是五个在XML文档中预定义好的实体:

Code sharing for xml parsing methods

实体必须以符号"&"开头,以符号";"结尾。 
注意: 只有"

--------------------------------------------------------------------------------

CDATA部件
在CDATA内部的所有内容都会被解析器忽略。

如果文本包含了很多的"

一个 CDATA 部件以""标记结束:

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>
</script>

在前面的例子中,所有在CDATA部件之间的文本都会被解析器忽略。

CDATA注意事项:
CDATA部件之间不能再包含CDATA部件(不能嵌套)。如果CDATA部件包含了字符"]]>" 或者"

同样要注意在字符串"]]>"之间没有空格或者换行符。

The above is the detailed content of Code sharing for xml parsing methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn