如何在搜索特定现有节点内的现有xml文件中插入节点,然后使用php插入子自定义节点

时间:2014-10-29 05:25:01

标签: php xml

    <?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="profile">
        <fieldset name="profile" label="PLG_USER_PROFILE_SLIDER_LABEL">
            <field name="address1" type="text" id="address1" description="PLG_USER_PROFILE_FIELD_ADDRESS1_DESC" filter="string" label="PLG_USER_PROFILE_FIELD_ADDRESS1_LABEL" size="30"/>
        </fieldset>
    <fields>
</from>

如何在现有节点字段中使用php插入节点。

我将使用波纹管代码进行插入,但不能正确插入节点。

 $xmlfile = "profiles/profile.xml"; 
                $dom = new DOMDocument();
                $dom->load($xmlfile);

                $findnode= $dom->getElementsByTagName("/fieldset")->item(0);
                $dom->documentElement->insertBefore($dom->createElement('section',"asdsad"),$findnode);
$dom->save($xmlfile);
  
    

它会运行,但会在表单节点内插入节点,而不是插入 fieldset 节点内。

  

1 个答案:

答案 0 :(得分:0)

是的,我会在下面的代码中找到解决方案.....

 $dom = new DOMDocument();
        $dom->load($xmlfile);   
        $ids = $dom->getElementsByTagName('fieldset')->item(0);

        $child = $dom->createElement('tagname');
        $child->appendChild($dom->createTextNode('some text'));
        $ids->appendChild($child);