simplexml编辑CDATA节点
我有一个 xml 文件, 我想打开它,使用 $_POST 输入中的值编辑某些 CDATA 节点并将其保存为同一文件, 我阅读了一些在线文档并最终来到这里, 有人请建议一个好的方法来做到这一点
......
I have an xml file,
I want to open it, edit certain CDATA node with the values from $_POST input and save it as same file,
I've read some online documentation and ended up here,
someone please suggest a nice way of doing this...
regardsh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 simples 函数扩展 SimpleXMLElement 类来执行此
用法:
结果:
You can extend class SimpleXMLElement with simples function to do this
usage:
result:
我建议你使用这个 http://www.php.net/manual/en/类.domdocument.php
I suggest you use this http://www.php.net/manual/en/class.domdocument.php
由于我最近遇到了同样的问题,我想让人们也看到一些代码,因为链接的示例只能添加新的 CDATA 部分,但不能删除旧的部分。因此,“我的”解决方案是从上述代码示例合并而来,并删除旧的 CDATA 节点。
Since I had the same issue just recently, I wanted to let people also see some code, because the linked examples can only add new CDATA sections, but do not remove the old ones. So "my" solutions is merged from the mentioned code example plus deleting the old CDATA node.
SimpleXML
默认情况下不会使 CDATA 元素可访问。您可以告诉 simplexml 跳过它们(默认)或读取它们(请参阅:从 rss feed 读取 cdata )。如果您阅读它们,它们是标准文本值,因此它们会与其他文本节点合并。文档对象模型文档,它提供了
DOMCdataSection
,它扩展了来自DOMText
,标准文本节点模型。尽管这是一个不同的 PHP 库(DOM 与 SimpleXML),但两者是相互兼容的。例如,
SimpleXMLElement
可以转换为DOMElement
通过使用dom_import_simplexml
函数。如果您发布一些到目前为止所做的代码,应该很容易弄清楚如何访问您想要修改的 CDATA 部分。请同时提供一些演示 XML 数据,以便示例更具说明性。
SimpleXML
does not make CDATA elements accessible by default. You can either tell simplexml to skip them (default) or to read them (see: read cdata from a rss feed). If you read them, they are standard text values, so they get merged with other textnodes.More control is offered by the Document Object ModelDocs, which offers a
DOMCdataSection
which extends fromDOMText
, the standard text node model.Even though this is a different PHP library (DOM vs. SimpleXML), both are compatible to each other. For example a
SimpleXMLElement
can be converted into aDOMElement
by using thedom_import_simplexml
function.If you post some code what you've done so far it should be easy to figure out how to access the CDATA sections you want to modify. Please provide as well some demo XML data so the example is more speaking.