simplexml编辑CDATA节点

发布于 2024-12-14 18:01:21 字数 117 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

勿忘初心 2024-12-21 18:02:07

您可以使用 simples 函数扩展 SimpleXMLElement 类来执行此

class ExSimpleXMLElement extends SimpleXMLElement {
    /**
     * Add CDATA text in a node
     * @param string $cdata_text The CDATA value  to add
     */
    private function addCData($cdata_text) {
        $node = dom_import_simplexml($this);
        $no = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($cdata_text));
    }

    /**
     * Create a child with CDATA value
     * @param string $name The name of the child element to add.
     * @param string $cdata_text The CDATA value of the child element.
     */
    public function addChildCData($name, $cdata_text) {
        $child = $this->addChild($name);
        $child->addCData($cdata_text);

        return $child;
    }

    /**
     * Modify a value with CDATA value
     * @param string $name The name of the node element to modify.
     * @param string $cdata_text The CDATA value of the node element.
     */
    public function valueChildCData($name, $cdata_text) {

        $name->addCData($cdata_text);

        return $name;
    }
}

用法:

$xml_string = <<<XML
        <root>
            <item id="foo"/>
        </root>
XML;

$xml5 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
$xml5->valueChildCData($xml5->item, 'mysupertext');
echo $xml5->asXML();

$xml6 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
$xml6->item->addChildCData('mylittlechild', 'thepunishment');
echo $xml6->asXML();

结果:

<?xml version="1.0"?>
<root>
  <item id="foo"><![CDATA[mysupertext]]></item>
</root>

<?xml version="1.0"?>
<root>
  <item id="foo">
    <mylittlechild><![CDATA[thepunishment]]></mylittlechild>
  </item>
</root>

You can extend class SimpleXMLElement with simples function to do this

class ExSimpleXMLElement extends SimpleXMLElement {
    /**
     * Add CDATA text in a node
     * @param string $cdata_text The CDATA value  to add
     */
    private function addCData($cdata_text) {
        $node = dom_import_simplexml($this);
        $no = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($cdata_text));
    }

    /**
     * Create a child with CDATA value
     * @param string $name The name of the child element to add.
     * @param string $cdata_text The CDATA value of the child element.
     */
    public function addChildCData($name, $cdata_text) {
        $child = $this->addChild($name);
        $child->addCData($cdata_text);

        return $child;
    }

    /**
     * Modify a value with CDATA value
     * @param string $name The name of the node element to modify.
     * @param string $cdata_text The CDATA value of the node element.
     */
    public function valueChildCData($name, $cdata_text) {

        $name->addCData($cdata_text);

        return $name;
    }
}

usage:

$xml_string = <<<XML
        <root>
            <item id="foo"/>
        </root>
XML;

$xml5 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
$xml5->valueChildCData($xml5->item, 'mysupertext');
echo $xml5->asXML();

$xml6 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
$xml6->item->addChildCData('mylittlechild', 'thepunishment');
echo $xml6->asXML();

result:

<?xml version="1.0"?>
<root>
  <item id="foo"><![CDATA[mysupertext]]></item>
</root>

<?xml version="1.0"?>
<root>
  <item id="foo">
    <mylittlechild><![CDATA[thepunishment]]></mylittlechild>
  </item>
</root>
一袭白衣梦中忆 2024-12-21 18:01:54

由于我最近遇到了同样的问题,我想让人们也看到一些代码,因为链接的示例只能添加新的 CDATA 部分,但不能删除旧的部分。因此,“我的”解决方案是从上述代码示例合并而来,并删除旧的 CDATA 节点。

// get DOM node
$node = dom_import_simplexml($mySimpleXmlElement); 


// remove existing CDATA ($node->childNodes->item(1) does not seem to work)
foreach($node->childNodes as $child) {
  if ($child->nodeType == XML_CDATA_SECTION_NODE) {
    $node->removeChild($child);
  }
}

// add new CDATA
$no = $node->ownerDocument; 
$node->appendChild($no->createCDATASection($myNewContent)); 

// print result
echo $xml->asXML();

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.

// get DOM node
$node = dom_import_simplexml($mySimpleXmlElement); 


// remove existing CDATA ($node->childNodes->item(1) does not seem to work)
foreach($node->childNodes as $child) {
  if ($child->nodeType == XML_CDATA_SECTION_NODE) {
    $node->removeChild($child);
  }
}

// add new CDATA
$no = $node->ownerDocument; 
$node->appendChild($no->createCDATASection($myNewContent)); 

// print result
echo $xml->asXML();
甜尕妞 2024-12-21 18:01:49

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 from DOMText, 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 a DOMElement by using the dom_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文