dongzangchui2072 2016-07-27 05:18
浏览 32

如何使用PHP从xml中的节点中提取特定标记?

Here is where I set up basic variables, such as creating the new DomDoc and such as well as loading some of the Tags. This all works fine at the moment.

  <?php 
if (isset($_GET['edit'])&& $_GET['edit']=='delete' && isset($_GET['id'])&&!empty($_GET['id'])){
        $dom = new DomDocument();
        $dom->preserveWhiteSpace = false;  
        $dom->load("data.xml");
        $root = $dom->documentElement;
        $record = $root->getElementsByTagName("data");
        $ID=$root->getElementsByTagName("ID");
        $nodetoremove = null;
        //$namenode=$root->getElementsByTagName("own_name");
        //$name="";
        //$datenode=$root->getElementsByTagName("sign_in");
        //$date="";
        $newid=$_GET['id'];
        foreach($ID as $node){

        $pid =$node->textContent;

Here I am checking if it's a new ID and if it is it does the following as seen.

    if ($pid == $newid) 
            { 
$nodetoremove=$node->parentNode;

            } 

    }

The issue is here. I am able to go through the selected node I wish to delete ($nodetoremove) and select a specific element (sign_in) but I am unsure how to so. Right now all I can do is go through and print all of the elements within the nodes of $nodetoremove. Is there a way I can get the element I want from XML this way?

    //Prints all information within $nodetoremove
        foreach ($nodetoremove->childNodes AS $item){
        print $item->nodeName . "=" . $item->nodeValue . "<br>";
        }
    foreach ($nodetoremove as $node) {
 }
    //Sets $name to the first Child of $nodetoremove
    $name=$nodetoremove->firstChild->nodeValue;


    //Checks if the nods to remove is not null, if it is removes $nodetoremove
    if($nodetoremove!=null){
        $root->removeChild($nodetoremove);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧