SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extending SimpleXML and DOM interoperability.

    This thread is an important follow up to the following threads:
    Accessing content and using iterated Objects in SimpleXML.

    Problem with XML in CDATA tag (swf related)

    Follow up code here:

    PHP Code:
    <?php
    /* Source for this example:  Robert Richards (2006):  Pro PHP XML and Web Services Chapter 7. */
    $book=simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml');
    $author $book->bookinfo->author;
    $children $author->children();
    foreach(
    $children AS $child) {
    /* Import node into DOM and get NodeName */
        
    $element dom_import_simplexml($child);
        
    $name $element->nodeName;
        print 
    $name": "$child '<br />';
    }
    /* 
    Extending SimpleXMLElement class.

    Note: A big difference with extended classes in SimpleXML from those in the DOM extension is that
    once an object using the extended class has been instanciated, all objects returned from the SimpleXML
    methods will use the extended class type.
     */
    class mySXE extends SimpleXMLElement {
        function 
    appendChild($name$content) {
            
    $dom dom_import_simplexml($this);
            
    $dom->appendChild($dom->ownerDocument->createElement($name$content));
        }
    }
    $sxe = new mySXE("<root><node1></node1></root>");
    $sxe->node1->appendChild("node2""content");
    print 
    '<br /> <br />';
    print 
    $sxe->asXML(); 
    ?>
    No problem with viewing the result in the latest versions of FireFox 2.0.0.4 and Opera 9.21. Not viewable in my latest version of IE 7.0.5730.11, because IE uses its own DOM API. In a real application you have to use feature sniffing, and modify the code where necessary, so the application degrades gracefully.

    Result here: http://www.kjellbleivik.com/SpHelp/morgy6

    Note that the DOM API has much richer functionality than the SimpleXML API. For PHP 5.0 and PHP 5.1 (at least) SimpleXML have no function / method that can be used to delete elements, although attributes (since they are uniqe within scope / namespace) can be deleted. Another aspect is that the SimpleXML API changes with updates of PHP, so it is not as stable as the DOM API. Therefore use this procedure:
    • Use the SimpleXML API when you are sure of it's functionality.
    • Extend its classes when added functionality is needed.
    • Use DOM when complex functionality is needed (and use it to test the SimpleXML API when you have time).
    • Keep an eye on updates of the API's with newer versions of PHP.
    Last edited by kgun; Jul 27, 2007 at 09:37.

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2007
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You are aware the XML markup on that page is invalid as per the XML spec right?

    You have CDATA and tags outside of the root element, plus no content comes before the XML processing instructions.

    The reason Firefox is rendering the page is because it's not interpreting the content as XML because the content type isn't sent as "text/xml" in the HTTP headers.

    Before any output functions are use by PHP you should use the header function:
    <?php
    header('Content-Type: text/xml');

    ... rest of code here ...
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the answer. This is not easy without xsl stylesheets.

    Quote Originally Posted by Speeple View Post
    Hi,
    You are aware the XML markup on that page is invalid as per the XML spec right?
    I am unsure. Isn't there a default markup when you do not specify it? I am aware of how important that and encoding is on a real site. This was a fast example. Great to get a discussion, since it is from that we learn.

    Quote Originally Posted by Speeple View Post
    Hi,
    You have CDATA and tags outside of the root element, plus no content comes before the XML processing instructions.
    You mean in this

    http://www.kjellbleivik.com/Books/Pr...ter07/sxml.xml

    file that is downloaded unmodified from the Apress site?.

    Quote Originally Posted by Speeple View Post
    Hi,
    The reason Firefox is rendering the page is because it's not interpreting the content as XML because the content type isn't sent as "text/xml" in the HTTP headers.

    Before any output functions are use by PHP you should use the header function:
    <?php
    header('Content-Type: text/xml');

    ... rest of code here ...
    ?>
    Here is the code for your proposal:

    PHP Code:
    <?php
    header
    ('Content-Type: text/xml');  //Only modification
    /* Source for this example:  Robert Richards (2006):  Pro PHP XML and Web Services Chapter 7. */
    $book=simplexml_load_file('http://www.kjellbleivik.com/Books/ProPHP/Chapter07/sxml.xml');
    $author $book->bookinfo->author;
    $children $author->children();
    foreach(
    $children AS $child) {
    /* Import node into DOM and get NodeName */
        
    $element dom_import_simplexml($child);
        
    $name $element->nodeName;
        print 
    $name": "$child '<br />';
    }
    /* 
    Extending SimpleXMLElement class.

    Note: A big difference with extended classes in SimpleXML from those in the DOM extension is that
    once an object using the extended class has been instanciated, all objects returned from the SimpleXML
    methods will use the extended class type.
     */
    class mySXE extends SimpleXMLElement {
        function 
    appendChild($name$content) {
            
    $dom dom_import_simplexml($this);
            
    $dom->appendChild($dom->ownerDocument->createElement($name$content));
        }
    }
    $sxe = new mySXE("<root><node1></node1></root>");
    $sxe->node1->appendChild("node2""content");
    print 
    '<br /> <br />';
    print 
    $sxe->asXML(); 
    ?>
    And here

    http://www.kjellbleivik.com/SpHelp/morgy61

    is the result.

    Same error in IE.

    Also error in FireFox.

    Opera, with the following options: View + Style + Author mode

    gives the best error message.

    "XML parsing failed: syntax error (Line: 1, Character: 0)

    Reparse document as HTML
    Error:unexpected text (non-whitespace text outside root element)
    Specification:http://www.w3.org/TR/REC-xml/
    1: firstname: Rob<br />surname: Richards<br /><br /> <br /><?xml version="1.0"?>
    2: <root><node1><node2>content</node2></node1></root>"


    When reparsed as HTML, the result is the same as in the first examle above.

    Have I misunderstood something?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2007
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I didn't understand your first post was a question.

    Is it a requirement you must extend SimpleXML with DOM?

    If you want a DOM example to parse that code I could post some.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it was not a question. My first post was a follow up of another post about problems with CDATA. May be you should post your solution in that post.

    It was not a real application with XSL styling etc.

    But it would be fine to see your example code. We all learn by seeing how other people solve a problem. So fine if you post it here.


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
  •