drqyxkzbs21968684 2013-12-18 14:55
浏览 82
已采纳

编辑XML文件(删除节点并添加新节点)

I have problems to deal with XML in PHP. What i want is to remove a not needed element and replace it with a other one.

Let's say the XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<software>
    <info>
        <version>1.2</version>
        <lasterror>1386680712</lasterror>
    </info>
    <decryption>
        <funcgroup siglength="86">
            <func>
                <name>Mk</name>
                <args>$a</args>
                <code>XXXX</code>
            </func>
            <func>
                <name>Nk</name>
                <args>$a,$b</args>
                <code>XXXX</code>
            </func>
        </funcgroup>
    </decryption>
</software>

PHP Code:

$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->loadXML(file_get_contents('test.xml'));

$thedocument = $domtree->documentElement;

$list = $thedocument->getElementsByTagName('funcgroup');
foreach ($list as $domElement) {
    $sig_length = $domElement->getAttribute('siglength');
    if($sig_length == $signature_length) {
        $domElement->parentNode->removeChild($domElement);
        break;
    }
}

$some_stuff = $domtree->getElementsByTagName('software');
$some_stuff = $domtree->getElementsByTagName('decryption');
$funcgroup = $domtree->appendChild($domtree->createElement('funcgroup'));
$funcgroup->setAttribute('siglength', $signature_length);

$func = $funcgroup->appendChild($domtree->createElement('func'));
$func->appendChild($domtree->createElement('name', $outer_element[0]));
$func->appendChild($domtree->createElement('args', $outer_element[1]));
$code = $func->appendChild($domtree->createElement('code'));
$code->appendChild($domtree->createTextNode($outer_element[2]));

Note: I removed some stuff otherwise it would get too complicated i guess. The above code shows what i do, but without some other loops and variables which are not needed in that question. Every variable (and array) is defined. So don't worry about that.

What i want is to remove the whole <funcgroup siglength="86"> in order to replace it with a different one.

The script works fine, but there is one problem in the output XML. It looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<software>
    <info>
        <version>6.3</version>
        <lasterror>1386680712</lasterror>
    </info>
    <decryption/>
</software>
<funcgroup siglength="86">
<func>
    <name>Nk</name>
    <args>$a</args>
    <code>YYYYY</code>
</func>
<func>
    <name>Ok</name>
    <args>$a,$b</args>
    <code>YYYY</code>
</func>
</funcgroup>

As you can see, the closing software and decryption tags are on the wrong place now.

How can i fix that? I spent hours but can't find a working solution.

The problem is caused by the removeChild() since it works fine if i do not remove something.

  • 写回答

1条回答 默认 最新

  • doulu4233 2013-12-18 15:06
    关注

    You are adding your new child node to the document itself (instead of the decryption node), which is not what you want

    $domtree->appendChild
    

    Instead you should:

    $decryption = $domtree->getElementsByTagName('decryption')->item(0);
    $funcgroup = $decryption->appendChild($domtree->createElement('funcgroup'));
    

    Edit:

    You can edit the text value of the lasterror node by doin:

    $domtree->getElementsByTagName('lasterror')->item(0)->firstChild->nodeValue = "New value";
    

    Consult the documentation of the DOMNodeList and DOMNode class to see what else you can do with it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序