使用PHP从XML中删除空标记

时间:2011-12-22 11:28:31

标签: php xml tags

问题

如何在PHP中删除空的xml标记?

实施例

 $value1 = "2";
 $value2 = "4";
 $value3 = "";

 xml = '<parentnode>
        <tag1> ' .$value1. '</tag1>
        <tag2> ' .$value2. '</tag2>
        <tag3> ' .$value3. '</tag3>
       </parentnode>';

XML结果:

<parentnode>
    <tag1>2</tag1>
    <tag2>4</tag2>
    <tag3></tag3> // <- Empty tag
</parentnode>

我想要的!

    <parentnode>
            <tag1>2</tag1>
            <tag2>4</tag2> 
    </parentnode>

没有像“tag3”这样的空标签的XML

谢谢!

5 个答案:

答案 0 :(得分:15)

您可以将XPathpredicate not(node())一起使用,以选择所有没有子节点的元素。

<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadxml('<parentnode>
    <tag1>2</tag1>
    <tag2>4</tag2>
    <tag3></tag3>
    <tag2>4</tag2>
    <tag3></tag3>
    <tag2>4</tag2>
    <tag3></tag3>
</parentnode>');

$xpath = new DOMXPath($doc);

foreach( $xpath->query('//*[not(node())]') as $node ) {
    $node->parentNode->removeChild($node);
}

$doc->formatOutput = true;
echo $doc->savexml();

打印

<?xml version="1.0"?>
<parentnode>
  <tag1>2</tag1>
  <tag2>4</tag2>
  <tag2>4</tag2>
  <tag2>4</tag2>
</parentnode>

答案 1 :(得分:6)

这是递归工作并删除以下节点:

  • 仅包含空格
  • 没有属性
  • 没有儿童笔记
// not(*) does not have children elements
// not(@*) does not have attributes
// text()[normalize-space()] nodes that include whitespace text
while (($node_list = $xpath->query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) && $node_list->length) {
    foreach ($node_list as $node) {
        $node->parentNode->removeChild($node);
    }
}

答案 2 :(得分:5)

$dom = new DOMDocument;

$dom->loadXML($xml);

$elements = $dom->getElementsByTagName('*');

foreach($elements as $element) {

   if ( ! $element->hasChildNodes() OR $element->nodeValue == '') {
       $element->parentNode->removeChild($element);
   }

} 

echo $dom->saveXML();

CodePad

答案 3 :(得分:2)

使用Xpath生成PHP SimpleXMLElement对象代码的解决方案是:

/*
 * Remove empty (no children) and blank (no text) XML element nodes, but not an empty root element (/child::*).
 * This does not work recursively; meaning after empty child elements are removed, parents are not reexamined.
 */
foreach( $this->xml->xpath('/child::*//*[not(*) and not(text()[normalize-space()])]') as $emptyElement ) {
    unset( $emptyElement[0] );
}

请注意,不需要使用PHP DOM,DOMDocument,DOMXPath或dom_import_simplexml()。

答案 4 :(得分:0)

如果您要做很​​多事情,请执行以下操作:

$value[] = "2";
$value[] = "4";
$value[] = "";   

$xml = '<parentnode>';
for($i=1,$m=count($value); $i<$m+1; $i++)
      $xml .= !empty($value[$i-1]) ? "<tag{$i}>{$value[$i-1]}</tag{$i}>" : null;
$xml .= '</parentnode>';
echo $xml;

理想情况下,您应该使用domdocument