duanliang5051 2018-02-28 18:27
浏览 77

使用PHP从xml中删除空节点

Below is my xml content and i want to return only non-emplty tags pls advice

$xml = '<template>' .
    '<height>' . $data['height'] . '</height>' .
    '<width>' . $data['height'] . '</width>' .
    '<text>' . $data['text'] . '</text>' .
    '</template>';

return $xml;

here the output

<template><height></height><width>ddddd</width><text></text>/template>
  • 写回答

1条回答 默认 最新

  • dongxing2302 2018-02-28 19:32
    关注

    Assuming that the OP intends for the height and width to be the same, as the OP's code is written, and if the OP's question only pertains to generating XML, you may code the following:

    <?php
    // suppose some data is missing...
    function generateXML(){
    $data = [];
    $data["height"]="";
    $data["text"]="just&nbsp;a&nbsp;test";
    
    
    $xml = "<template>";
    $xml .= empty($data["height"])? "" : "<height>$data[height]</height><width>$data[height]</width>";
    
    $xml .= empty($data["text"])? "" : "<text>$data[text]</text>";
    
    $xml .= "</template>";
    return $xml;
    }
    echo generateXML();
    ?>
    

    See live code

    In this example $data["height"] is set to an empty string. It could also be set to NULL. If it is not set at all, empty() will still work but a notice will appear complaining about an undefined index "height"; see here.

    If the question instead pertains to XML that already exists, then one may use a heredoc and PHP's support for the Document Object Model (DOM), as follows:

    <?php
    
    // suppose some data is missing...
    $data = [];
    $data["height"]="";
    $data['text']="more testing";
    
    // get XML into a variable ...
    $xml = <<<XML
        <template>
            <height>$data[height]</height>
            <width>$data[height]</width>
            <text>$data[text]</text>
        </template>
    XML;
    
    
        $dom = new DOMDocument;
        $dom->preserveWhiteSpace = false;
        $dom->loadXML( $xml );
        $template = $dom->getElementsByTagName('template')->item(0); 
        $nodeList = $template->childNodes;
    
        echo (function() use($dom,$template,$nodeList){
           // iterate backwards to remove node missing value 
           for( $max=$nodeList->length-1, $i=0; $max >= $i; $max-- ) {
              $currNode = $nodeList->item($max);
              $status = $currNode->hasChildNodes()? true:false;
              if ($status === false) {
                 $currNode->parentNode->removeChild( $currNode );
              }// end if
           }// end for
           return $dom->saveXML( $template );
        })(); // immediate executable
    

    See live code

    This example also makes use of a PHP 7 feature, an immediately invoked function expression (iffe).

    Caveat: If you use an associative array with a heredoc, avoid using quotes of any kind around the element's name, otherwise your code will yield an error; good read here

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。