gpt4 book ai didi

php - 解析 xml 文件时保留 html 内容

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:52 26 4
gpt4 key购买 nike

我有一个xml文件

<text>
<defalut>This file has the following features:</defalut>
<value>
<ul>
<li><info>CSS text formatting </info></li>
<li><info>Text loaded from a XML</info></li>
<li><info>Scrolls with easing</info></li>
<li><info>Mouse wheel supported</info></li>
<li><info>HTML supported</info></li>
<li><info>Click on the bar to move the handle to that point</info></li>
<li><info>Supports images</info></li>
<li><info>The scrollbar hides if not needed</info></li>
<li><info>The scrollbar resizes proportonal to the text sizeq</info></li>
</ul>
</value>
<tittle>Lorem Ipsum</tittle>
</text>

我正在使用 xpath 和 xquery 来解析这个文件

$xml_str1 = file_get_contents("file.xml");
echo $xml_str = $xml_str.$xml_str1;
$xml = simplexml_load_string($xml_str);
$nodes = $xml->xpath('//text');

但我只获取字符串。我怎样才能在结果中保留 html 标签 如果我正在解析 <value>标签我得到的内容没有 <ul><li>我怎样才能获得 <value> 中的完整 html 内容?按原样标记

提前致谢

最佳答案

我不是 %100 确定我解决了您的问题,但我的直觉告诉我您正在寻找 CDATA 部分:

<text>
<defalut>This file has the following features:</defalut>
<value>
<![CDATA[
<ul>
<li><info>CSS text formatting </info></li>
<li><info>Text loaded from a XML</info></li>
<li><info>Scrolls with easing</info></li>
<li><info>Mouse wheel supported</info></li>
<li><info>HTML supported</info></li>
<li><info>Click on the bar to move the handle to that point</info></li>
<li><info>Supports images</info></li>
<li><info>The scrollbar hides if not needed</info></li>
<li><info>The scrollbar resizes proportonal to the text size</info></li>
</ul>
]]>
</value>
<tittle>Lorem Ipsum</tittle>
</text>

或者你可以逃避所有这些 <>字符:

<text>
<defalut>This file has the following features:</defalut>
<value>
&lt;ul&gt;
&lt;li&gt;&lt;info&gt;CSS text formatting &lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;Text loaded from a XML&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;Scrolls with easing&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;Mouse wheel supported&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;HTML supported&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;Click on the bar to move the handle to that point&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;Supports images&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;The scrollbar hides if not needed&lt;/info&gt;&lt;/li&gt;
&lt;li&gt;&lt;info&gt;The scrollbar resizes proportonal to the text size&lt;/info&gt;&lt;/li&gt;
&lt;/ul&gt;
</value>
<tittle>Lorem Ipsum</tittle>
</text>

关于php - 解析 xml 文件时保留 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3459909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com