在PHP中修改![CDATA []] (XML) [英] Modify ![CDATA[]] in PHP? (XML)

查看:137
本文介绍了在PHP中修改![CDATA []] (XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含![CDATA []]数据的XML文件。像这样:

 < link>< ;! [CDATA [https://google.de]]> /链路> 

现在我听说我无法修改![CDATA []]数据,或者它们包含一些特殊的字符。但我不记得了...
这就是为什么我在这里问问的原因。



我可以更改值![CDATA []]如果是,如何?



我只想在链接上添加类似?= dadc的内容。



编辑:
我的XML文件结构(想要编辑url):

 <?xml version =1.0encoding =UTF-8?> 
< rss>
< channel xmlns:g =http://base.google.com/ns/1.0version =2.0>
< title> Google Eur English 1< / title>
< description />
< item>
< title> Anno 2070< / title>
< g:image_link>< ;! [CDATA [http://cdn.kinguin.net/media/catalog/category/anno_8.jpg]]>< / g:image_link>
< url>< ;! [CDATA [http://www.kinguin.net/category/4/anno-2070/?nosalesbooster=1&country_store=1&currency=EUR]]>< / URL>
< price><![CDATA [3.88 EUR]]>< / price>
< platform> Uplay< / platform>
< / item>
< / channel>
< / rss>

问候语

解决方案

对于SimpleXML,这是正确的。 CDATA节是一种特殊的文本节点。它们实际上在这里使嵌入式部件更易于人体使用。 SimpleXML并不真正处理XML节点,所以你必须将它转换成标准的文本节点。



如果你在XML中有一个JS或HTML片段,如果没有转义特殊字符,例如< ,请阅读。这就是CDATA部分(和浏览器的一些向后兼容性)。



所以要修改CDATA部分并保留它,您将不得不使用DOM。 DOM实际上知道不同的节点类型。以下是一个小例子:

  $ xml ='< link><![CDATA [https:// google。 DE]] GT;< /链路>'; 

$ document = new DOMDocument();
$ document-> loadXml($ xml);
$ xpath = new DOMXpath($ document);

foreach($ xpath-> evaluate('// link / text()')as $ linkValue){
$ linkValue-> data。='?abc'
}
echo $ document-> saveXml();

输出:

 code><?xml version =1.0?> 
< link><![CDATA [https://google.de?abc]]>< / link>


I have an XML file which contains ![CDATA[]] data. Like this:

<link><![CDATA[https://google.de]]></link>

Now I heard that I can not modify ![CDATA[]] data or that they contains some special characters. But I do not remember anymore... That's the reason why I'am asking here.

Can I change the values in ![CDATA[]] and if yes, how?

I just want to append something like "?=dadc" on the link.

Edit: My XML file structure (Want to edit the url):

<?xml version="1.0" encoding="UTF-8"?>
    <rss>
      <channel xmlns:g="http://base.google.com/ns/1.0" version="2.0">
        <title>Google Eur English 1</title>
        <description/>
        <item>
          <title>Anno 2070</title>
          <g:image_link><![CDATA[http://cdn.kinguin.net/media/catalog/category/anno_8.jpg]]></g:image_link>
          <url><![CDATA[http://www.kinguin.net/category/4/anno-2070/?nosalesbooster=1&country_store=1&currency=EUR]]></url>
          <price><![CDATA[3.88 EUR]]></price>
          <platform>Uplay</platform>
        </item>
      </channel>
    </rss>

Greetings

解决方案

That is true for SimpleXML. CDATA Sections are a special kind of text nodes. They are actually here to make embedded parts more readable for humans. SimpleXML does not really handle XML nodes so you will have to let it convert them to standard text nodes.

If you have a JS or HTML fragment in XML it is easier to read if the special characters like < are not escaped. And this is what CDATA sections are for (and some backwards compatibility for browsers).

So to modify a CDATA section and keep it, you will have to use DOM. DOM actually knows about the different node types. Here is a small example:

$xml = '<link><![CDATA[https://google.de]]></link>';

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//link/text()') as $linkValue) {
  $linkValue->data .= '?abc';
}
echo $document->saveXml();

Output:

<?xml version="1.0"?>
<link><![CDATA[https://google.de?abc]]></link>

这篇关于在PHP中修改![CDATA []] (XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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