【问题标题】:How to set text content with SimpleXML's XPath support?如何使用 SimpleXML 的 XPath 支持设置文本内容?
【发布时间】:2013-09-23 17:49:46
【问题描述】:
<xml>
    <ns:foo attribute="bar">
        OLD TEXT DATA
    </ns:foo>
</xml>

我使用 XPath 来处理 XML 名称空间。这行不通:

$xml->asXML('old.xml');

foreach ($xml->xpath('ns:foo') as $foo) {
    $foo['attribute'] = 'new bar';
    $foo = 'NEW TEXT DATA'; //This won't be saved by asXML().
}

$xml->asXML('new.xml');

如何使用SimpleXMLElement::xpath 更改文本内容?

【问题讨论】:

    标签: php xml xpath simplexml


    【解决方案1】:
    $xml->asXML('old.xml');
    
    foreach ($xml->xpath('ns:foo') as $foo) {
        $foo['attribute'] = 'new bar';
        $foo[0] = 'NEW TEXT DATA';
    }
    
    $xml->asXML('new.xml');
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2011-11-11
      • 2012-07-31
      • 2018-12-04
      • 1970-01-01
      相关资源
      最近更新 更多