SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Coeur d Alene, ID
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having Trouble Properly Adding Items to Newsfeed with PHP Script

    Hello, all.
    I'm having trouble with a PHP script I'm writing to update an RSS newsfeed. It adds a new <item> to the newsfeed but it adds it at the bottom instead of at the top of the list. How can I have it add the new <item> as the first one on the list instead of the last?

    Here is the script:
    Code:
    <?php
    $xml = simplexml_load_file('test.xml');
    $cat1 = 'cat one';
    $descrip = 'The description.';
    $perma = time() . 'mysite-dot-net' . rand(0, 10000);
    $TheLink = 'http://www.example.com/';
    $ThePubDate = strftime('%a, %d %b %Y %H:%M:%S %z');
    $TheTitle = 'The New Title 3';
    $NewOne = $xml->channel->addChild('item');
    $NewOne->addChild('category', $cat1);
    $NewOne->addChild('description', $descrip);
    $NewOne->addChild('guid', $perma);
    $NewOne->addChild('link', $TheLink);
    $NewOne->addChild('pubDate', $ThePubDate);
    $NewOne->addChild('title', $TheTitle);
    $xml->asXML('test.xml');
    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,498
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)

    insert

    Hi Tachyon80, welcome to the forums,
    I don't think simplexml has an easy way to do this, maybe you should try the DOM XML functions, ie.
    DomNode->insert_before ($newnode, $refnode);

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Coeur d Alene, ID
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't make much progress with this.

    Code:
    $elements = $dom_xml->getElementsByTagName("item");
    $element = $elements->item(0);
    $NewNode = $element->insertBefore($element, $element);
    $elements is a DOMNodeList with 10 items. I want to zero in on the first item (index "0") so I try to do that with the 2nd line of code. But the 3rd line of code keeps giving me errors. I want to insert a new <item></item> just before the first item.

    I get this:
    Fatal error: Uncaught exception 'DOMException' with message 'Hierarchy Request Error'

    What does that even mean? Can anyone tell me what I'm doing wrong? I'm guessing I should pass different arguments to the insertBefore command but how do I know which arguments to pass?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,498
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)

    error

    I think the 'Hierarchy Request Error' has something to do with tag nesting level. My guess (after looking at the online documentation) is the code should be more like
    PHP Code:
    $elements $dom_xml->get_elements_by_tagname("item");
    $element $elements[0];
    $NewNode $element->insert_before($element$element); 

  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Coeur d Alene, ID
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for looking at it.
    get_elements_by_tagname is an unknown method that works when replaced with getElementsByTagName
    $element = $elements[0]; results in this:
    Fatal error: Cannot use object of type DOMNodeList as array in G:\mysite.net\html\updates\testr.php on line 24

    insert_before isn't recognized but insertBefore is.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,498
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)

    object

    What is the $dom_xml object?
    It sounds like you are using the DOM (PHP ver 5) functions instead of the DOM XML (PHP ver 4+) functions. Sorry, I was thinking DOM XML.
    I noticed this in the documentation
    Quote Originally Posted by Jerry Ellis
    03-Jun-2005 12:45
    1st argument) a node to insert
    2nd argument) a reference node - this is the node that the new node will be inserted before

    The trick to using this method is that the OBJECT on which you actually CALL the insertBefore() method is actually the PARENT node of the reference node!

    INCORRECT:
    $DOMNode_refNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);

    CORRECT:
    $DOMNode_refNode->parentNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);
    Might this have a part in the problem?

  7. #7
    SitePoint Member
    Join Date
    Aug 2007
    Location
    Coeur d Alene, ID
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My script is running now and doing what I wanted. One question, though: now that I know how to clone an existing node:
    $element->cloneNode(true)

    How do I create a new one? For example, a new <item></item> (it doesn't have to have anything between the tags).

    My working script:
    Code:
    <?php
    $xml = simplexml_load_file('test.xml');
    $cat1 = 'cat one';
    $cat2 = 'cat two';
    $descrip = 'The description.';
    $perma = time() . 'gearsonline-dot-net' . rand(0, 10000);
    $TheLink = 'http://www.example.com/';
    putenv('TZ=PST8PDT');
    $ThePubDate = strftime('%a, %d %b %Y %H:%M:%S %z');
    $TheTitle = 'The New Title';
    
    $dom_xml = dom_import_simplexml($xml);
    $elements = $dom_xml->getElementsByTagName("item");
    $element = $elements->item(0);
    $parent = $dom_xml->getElementsByTagName("channel")->item(0);
    $NewNode = $parent->insertBefore($element->cloneNode(true), $element);
    $xml = simplexml_import_dom($dom_xml);
    
    $xml->channel->item[0]->category[0] = $cat1;
    $xml->channel->item[0]->category[1] = $cat2;
    $xml->channel->item[0]->description = $descrip;
    $xml->channel->item[0]->guid = $perma;
    $xml->channel->item[0]->link = $TheLink;
    $xml->channel->item[0]->pubDate = $ThePubDate;
    $xml->channel->item[0]->title = $TheTitle;
    
    unset($xml->channel->item[10]);
    $xml->asXML('test.xml');
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •