Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:54:43+00:00 2026-06-11T22:54:43+00:00

I have been asking a bit about PHP and XML here at StackOverflow and

  • 0

I have been asking a bit about PHP and XML here at StackOverflow and are getting the grasp of it thanks to all your coders, but sometimes I get stuck.

I want to be able to delete a Node if two children is set correct? I have been trying with this:

$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>";

I want to delete all the games where turn is below 2 and infopop = 1. In this case It should delete the last game… But it doenst work??? What am I doing wrong?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:54:44+00:00Added an answer on June 11, 2026 at 10:54 pm

    Personally, I have found using SimpleXML much easier than the DOMDocument when dealing with XML in PHP. However, here’s now you’ll make your code work. 🙂

    <?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";
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been thinking quite a bit here lately about screen scraping and what
I have been checking out some info about 64-bit driver development; I found that
I have been here before, asking for a mapping library that could store objects
Alright as I have been asking the last couple days and inching closer and
I know there have been a million questions asking this, but mine is different.
Have been trying to encrypt an xml file to a string so that I
Have been reading about async and tasks and been attempting to convert the CopyFileEx
Have have been trying to make a validator for my xml files. I have
I have been programming iPhone SDK for around 6 months but am a bit
So I know there have been a couple of posts around about this topic,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.