0

我在 StackOverflow 上询问了一些关于 PHP 和 XML 的问题,感谢你们所有的编码人员,我已经掌握了它,但有时我会卡住。

如果两个孩子设置正确,我希望能够删除一个节点?我一直在尝试这个:

$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
  <game id="86102">
    <opponent>shoooorty</opponent>
    <oppid>2512</oppid>
    <lastdraw>2</lastdraw>
    <turn>1</turn>
    <image>noimage.png</image>
    <nextdraw>6198</nextdraw>
    <infopop>0</infopop>
    <playertilesum>73</playertilesum>
    <oppnation>0</oppnation>
  </game>
  <game id="88341">
    <opponent>Jmemek</opponent>
    <oppid>1917</oppid>
    <lastdraw>3</lastdraw>
    <turn>2</turn>
    <image>1917a.png</image>
    <nextdraw>3107</nextdraw>
    <infopop>1</infopop>
    <playertilesum>27</playertilesum>
    <oppnation>0</oppnation>
  </game>
  <game id="88382">
    <opponent>Gitteloven</opponent>
    <oppid>3153</oppid>
    <lastdraw>1</lastdraw>
    <turn>1</turn>
    <image>noimage.png</image>
    <nextdraw>2953</nextdraw>
    <infopop>1</infopop>
    <playertilesum>19</playertilesum>
    <oppnation>0</oppnation>
  </game>
</data>
');

// original
echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";

$opNodes = $xml->getElementsByTagName('turn');
$opNodes2 = $xml->getElementsByTagName('infopop');
foreach($opNodes as $node){
    $xmlTurn = trim($node->nodeValue);
    foreach($opNodes2 as $node2){
        $xmlPopup = trim($node2->nodeValue);
        if($xmlTurn < 2 && $xmlPopup == 1){
            $gameNode = $node->parentNode;
            $gameNode->parentNode->removeChild($gameNode);
        }
    }
}

echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";

我想删除 turn 低于 2 且 infopop = 1 的所有游戏。在这种情况下,它应该删除最后一个游戏......但它不起作用???我究竟做错了什么?

提前致谢!

4

1 回答 1

0

就个人而言,我发现在 PHP 中处理 XML 时,使用 SimpleXML 比使用 DOMDocument 容易得多。但是,现在您将使您的代码工作。:)

<?php
$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
  <game id="86102">
    <opponent>shoooorty</opponent>
    <oppid>2512</oppid>
    <lastdraw>2</lastdraw>
    <turn>1</turn>
    <image>noimage.png</image>
    <nextdraw>6198</nextdraw>
    <infopop>0</infopop>
    <playertilesum>73</playertilesum>
    <oppnation>0</oppnation>
  </game>
  <game id="88341">
    <opponent>Jmemek</opponent>
    <oppid>1917</oppid>
    <lastdraw>3</lastdraw>
    <turn>2</turn>
    <image>1917a.png</image>
    <nextdraw>3107</nextdraw>
    <infopop>1</infopop>
    <playertilesum>27</playertilesum>
    <oppnation>0</oppnation>
  </game>
  <game id="88382">
    <opponent>Gitteloven</opponent>
    <oppid>3153</oppid>
    <lastdraw>1</lastdraw>
    <turn>1</turn>
    <image>noimage.png</image>
    <nextdraw>2953</nextdraw>
    <infopop>1</infopop>
    <playertilesum>19</playertilesum>
    <oppnation>0</oppnation>
  </game>
</data>
');

// original
echo "<xmp>OLD:\n". $xml->saveXML()."</xmp>";

print "number of games: ".$xml->getElementsByTagName("game")->length."<br />\n";

// get the list of games and store the length param
$games = $xml->getElementsByTagName("game");
$len   = $games->length;

// loop the games
for( $i=0; $i<$len; $i++ )
{
    // reset tracking for whether or not to delete this game
    $turn_lt_2 = false;
    $infopop_eq_1 = false;

    // get the game node
    $game = $games->item($i);
    // loop the game's child nodes and look for the turn and infopop
    foreach( $game->childNodes as $node )
    {
        switch( $node->nodeName )
        {
            case "turn":
                if( (int)$node->nodeValue < 2 ) { $turn_lt_2 = true; }
            break;

            case "infopop":
                if( (int)$node->nodeValue === 1 ) { $infopop_eq_1 = true; }
            break;
        }
    }

    // check to see if both conditions were met
    if( ($turn_lt_2 === true) && ($infopop_eq_1 === true) )
    {
        print "deleting game <b>[".$i."]</b><br />\n";
        $game->parentNode->removeChild($game);
    }
}

echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
print "new number of games: ".$games->length."<br />\n";
?>
于 2012-09-25T21:58:15.617 回答