0

这是我正在工作的 xml 输出

   <adamContent projectTypeID="10" genContentID="000002">
        <metaData>
            <keywordList>
                      <keyword>
                            <word>#27</word>
                            <relevancy>120</relevancy>
                      </keyword>
                      <keyword>
                            <word>#43</word>
                            <relevancy>114</relevancy>
                      </keyword>
             </keywordList>
            <taxonomy type="CPT">
               <code>99374</code>
            </taxonomy>
        </metaData>
         <textContent title="Description" ordinal="0" group="0">
             <p>An in-depth report on the causes, diagnosis, and treatment of Alzheimer's disease.</p>
        </textContent>
</adamContent>

我需要这样的输出(我想删除整个元数据节点

<adamContent projectTypeID="10" genContentID="000002">
         <textContent title="Description" ordinal="0" group="0">
             <p>An in-depth report on the causes, diagnosis, and treatment of Alzheimer's disease.</p>
        </textContent>
</adamContent>

这是我到目前为止尝试的 php 代码

<?php
$xmldoc = new DOMDocument();
$atvID =   $xmldoc->load('000002.xml', LIBXML_NOBLANKS);
foreach($atvID as $id){
    $delnode = $xmldoc->getElementsByTagName('metaData');
    $xmldoc->firstChild->removeChild($delnode->item($id));
}
$xmldoc->save('000002.xml');
 ?>
4

1 回答 1

3
$xmldoc = new DOMDocument();
$xmldoc->load('000002.xml');
$metadatas = $xmldoc->getElementsByTagName('metaData');
foreach ($metadatas as $metadata) {
    $metadata->parentNode->removeChild($metadata);
}
$xmldoc->save('000002.xml');
于 2012-11-02T13:06:47.660 回答