我在 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 的所有游戏。在这种情况下,它应该删除最后一个游戏......但它不起作用???我究竟做错了什么?
提前致谢!