开发者

SimpleXML in PHP; Replace node with text node

Given this XML/XHTML snippet:

<h1>
    <zuq:data name="siteHeader" />
</h1>

<h2>
    <zuq:data name="pageHeaderName" />
    <span>&mdash;</span>
    <zuq:data name="pageHea开发者_运维知识库derTitle" />
</h2>

I've used SimpleXML's XPath method to round up all top-level nodes in the zuq namespace. Since the array elements of the xpath() method are pseudo references to the SimpleXML object tree nodes, I figured manipulation would be easy. However, I cannot figure out how to replace a given element node with a text node. How could I, for example, replace <zuq:data name="siteHeader" /> with the text My Site Header.

I've considered simply targeting the parent node and modifying it's contents as could work with the first block (<h1>), but I don't see that working given the case of my second block (<h2>).

Is there an easy way to replace a given element node with a text node via SimpleXML in PHP?


as far as i know, simplexml doesn't have the capability to remove or replace nodes. it's more for reading, creating, and non-structure related edits. you will have to use a DOM XML object to fully replace nodes. it's sort of like a more complex version of simplexml. read about it here:

http://www.php.net/manual/en/book.domxml.php


Quickly looking at the documentation I was not able to find a simple way either. I would try to just iterate over the original xml doc and copy each element to a new xml doc, modifying them as necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜