从simpleXML中删除节点

时间:2011-02-02 23:55:06

标签: php web-config simplexml unset

我正在尝试从web.config文件中取消设置节点,但它似乎没有工作。谁知道我做错了什么?如果有更好的方法请告诉我?

            $web_config = simplexml_load_file('web.config');

            $nodes = $web_config->children();

            $att_name = 'myMap';
            $value = '1';

            $map_node = $nodes[0]->xpath( sprintf('rewrite/rewriteMaps/rewriteMap[@name="%s"]/add[@value="%d"]', $att_name, $value) );

            print_r($map_node); // this outpus the correct node

            if (!empty($map_node)) {
                unset($map_node)
            } else {
                printf('No maps with value: "%d" found', $value);
            }

            $web_config->asXML(); 

1 个答案:

答案 0 :(得分:-1)

$web_config = new SimpleXMLElement('web.config',null,true);
$map_node = $web_config->xpath( sprintf('//rewrite/rewriteMaps/rewriteMap[@name="%s"/add[@value="%d"]', 'myMap', 1) );

if (!empty($map_node)) {
    unset($map_node[0][0]);
}

$web_config->asXml()