[DOM XML] How to append a link element as a child

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Marc Molina

    #1

    [DOM XML] How to append a link element as a child

    Hello,

    I'm trying to generate a RSS newsfeed using the DOM XML functions, but you
    already know that if you read my previous post .

    I'm able to create rss, channel and title elements but not a link element.
    The following code sample doesn't generated
    <link>http://www.websitetitl e.com</link> but <link> like if the DOM XML
    forgot to close the element :

    <?php

    $link_el = $dom_doc->create_eleme nt ('link');
    $link_el = $channel_el->append_child ($link_el);
    $link_el->set_content ('http://www.websitetitl e.com');

    ?>

    Then I got some crazy idea. I thought that maybe DOM XML was thinking the
    link element I created was a specially element, not an enclosed one, like in
    XHTML for example :

    <link rel="stylesheet " type="text/css" href="styles.cs s" />

    Crazy crazy because I don't see why DOM XML would handle my elements as HTML
    elements... But then I tried with a meta tag, and guess what... It also
    doesn't work ! It works with enclosed elements, tags, like title, head,
    html, body or unknown ones like channel or item. But it doesn't work with
    link, meta and input elements !

    And I have no clue why it handles these elements as special elements...

    Here is the full script :

    <?php

    header ('content-type: text/xml');

    echo ('<?xml version="1.0" encoding="ISO-8859-15"?>');

    $dom_doc = domxml_new_doc ('1.0');

    $rss_el = $dom_doc->create_eleme nt ('rss');
    $rss_el->set_attribut e ('version', '2.0');
    $rss_el = $dom_doc->append_child ($rss_el);

    $channel_el = $dom_doc->create_eleme nt ('channel');
    $channel_el = $rss_el->append_child ($channel_el);

    $title_el = $dom_doc->create_eleme nt ('title');
    $title_el = $channel_el->append_child ($title_el);
    $title_el->set_content ('Website title - News');

    $link_el = $dom_doc->create_eleme nt ('link');
    $link_el = $channel_el->append_child ($link_el);
    $link_el->set_content ('http://www.websitetitl e.com');

    echo ($dom_doc->html_dump_me m ());

    ?>

    If you rename 'link' to 'link2', it works !

    JM

  • Jean-Marc Molina

    #2
    Re: [DOM XML] How to append a link element as a child

    Jean-Marc Molina a écrit/wrote :[color=blue]
    > echo ($dom_doc->html_dump_me m ());[/color]

    Solution : Use the dump_mem method, not the html_dump_mem one.

    --
    Jean-Marc.

    Comment

    Working...