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 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单