Problems with DOM XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Problems with DOM XML

    Hey guys
    Im trying to build a plugin for a php script. Its going fine and
    dandy.

    Data is store in an XML file. Which has the following structure:

    <raiders>
    <raider>
    <name>Anarchi e</name>
    <ready>?</ready>
    </raider>
    </raiders>

    What i need to do is be able to find a "raider" and change the ready
    node to something else.

    For this i wrote this code snippet:
    -

    $doc = new DomDocument();
    $doc->load($DataDir. "/raid.bot/raid.xml");

    $raiders = $doc->getElementsByT agName("raider" );
    foreach ($raiders as $raider)
    {
    $names = $raider->getElementsByT agName("name");
    $name = $names->item(0)->nodeValue;
    if($name = $sender)
    {
    $statuses = $raider->getElementsByT agName("ready") ;
    $ready = $statuses->item(0)->setContent("ye s");
    }
    }
    $doc->save($DataDir. "/raid.bot/raid.xml");
    send_reply("Upd ated status to ready");

    -
    However it complains about the "set content" method:
    PHP Fatal error: Call to undefined method DOMElement::set Content() in
    F:\Games\
    guildBot\plugin s\tell\raid.bot \index.php on line 89

    I have tried set_content() aswell.

    Any ideas what im doing wrong here? Is set_content really the only way
    to update a node in DOM xml?

    Thoughts, ideas and suggestions are welcome.

    - Mike
  • Sjoerd

    #2
    Re: Problems with DOM XML

    On Aug 18, 1:54 pm, Michael <michael.fr...@ gmail.comwrote:
    I have tried set_content() aswell.
    Maybe this comment on the PHP site can help you:

    Comment

    • Michael

      #3
      Re: Problems with DOM XML

      On 18 Aug., 14:46, Sjoerd <sjoer...@gmail .comwrote:
      On Aug 18, 1:54 pm, Michael <michael.fr...@ gmail.comwrote:
      >
      I have tried set_content() aswell.
      >
      Maybe this comment on the PHP site can help you:http://nl2.php.net/manual/en/functio...tent.php#30324
      I figured it was becouse i was using deprecated features. That
      explained a lot :).

      Trying to get this to work now:
      send_reply("sta rting DOM");
      $doc = new DomDocument();
      $doc->load($DataDir. "/raid.bot/raid.xml");

      $raiders = $doc->getElementsByT agName("raider" );
      foreach ($raiders as $raider)
      {
      $names = $raider->getElementsByT agName("name");
      $name = $names->item(0)->nodeValue;
      if($name = $sender)
      {
      $newready = $doc->createElement( "ready");
      $newreadytext = $doc->createTextNode ("yes");
      $newready->appendChild($r eadytext);
      $ready = $raider->getElementsByT agName("ready")->item(0);
      $raider->replaceChild($ newready,$ready );
      $doc->save($DataDir. "/raid.bot/raid.xml");
      send_reply("Upd ated status to ready");
      }
      }

      No errors and it gets as far as to get into the loop. Nothing happens
      after that though.

      Comment

      • Michael

        #4
        Re: Problems with DOM XML

        On 18 Aug., 15:13, Michael <michael.fr...@ gmail.comwrote:
        On 18 Aug., 14:46, Sjoerd <sjoer...@gmail .comwrote:
        >
        On Aug 18, 1:54 pm, Michael <michael.fr...@ gmail.comwrote:
        >
        I have tried set_content() aswell.
        >
        Maybe this comment on the PHP site can help you:http://nl2.php.net/manual/en/functio...tent.php#30324
        >
        I figured it was becouse i was using deprecated features. That
        explained a lot :).
        >
        Trying to get this to work now:
        send_reply("sta rting DOM");
        $doc = new DomDocument();
        $doc->load($DataDir. "/raid.bot/raid.xml");
        >
        $raiders = $doc->getElementsByT agName("raider" );
        foreach ($raiders as $raider)
        {
        $names = $raider->getElementsByT agName("name");
        $name = $names->item(0)->nodeValue;
        if($name = $sender)
        {
        $newready = $doc->createElement( "ready");
        $newreadytext = $doc->createTextNode ("yes");
        $newready->appendChild($r eadytext);
        $ready = $raider->getElementsByT agName("ready")->item(0);
        $raider->replaceChild($ newready,$ready );
        $doc->save($DataDir. "/raid.bot/raid.xml");
        send_reply("Upd ated status to ready");
        }
        >
        }
        >
        No errors and it gets as far as to get into the loop. Nothing happens
        after that though.
        i solved it.

        $parent = new DomDocument;
        $parent_node = $parent ->createElement( 'raider');
        $parent_node->appendChild($p arent->createElement( 'name', $sender));
        $parent_node->appendChild($p arent->createElement( 'ready', 1));
        $parent->appendChild($p arent_node);

        $dom = new DomDocument;
        $dom->load($DataDir. "/raid.bot/raid.xml");

        $xpath = new DOMXpath($dom);
        $nodelist = $xpath->query('/raiders/raider');
        $i = 0;
        foreach($nodeli st as $nodes)
        {
        $names = $nodes->getElementsByT agName("name");
        $name = $names->item(0)->nodeValue;
        $i++;
        if($name == $sender)
        {
        break;
        }
        }
        $oldnode = $nodelist->item($i-1); //Dirty hack
        $newnode = $dom->importNode($pa rent->documentElemen t, true);
        $oldnode->parentNode->replaceChild($ newnode, $oldnode);
        $dom->save($DataDir. "/raid.bot/raid.xml");

        Comment

        Working...