SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member k3nny's Avatar
    Join Date
    Mar 2004
    Location
    Pretoria
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP 5 DOM Newbie

    I've been coding PHP for quite a while now, but never really needed to learn to use the DOM functions to parse XML. My problem is with PHP5, almost everything in the manual doesn't work anymore.

    I found a good tutorial at the International PHP Magazine, , which helped to clear out some of the most simplest things. I then wrote some code to discover all the available classes and methods in the PHP5 dom extension:

    PHP Code:
    // Get all our classes
    $classes get_declared_classes ();

    // new array for our dom classes
    $dom = array ();

    // Find all the dom classes
    foreach ($classes as $c)
    {
        if (
    preg_match ('/dom/i'$c))
            
    $dom[] = $c;
    }

    // sort the array
    sort($dom);

    // Print a list of all the methods of each dom class
    foreach ($dom as $d)
    {
        echo 
    "<table border=\"1\"><tr><th>Class: $d</th></tr><tr><td><ul>\n";
        
        
    // get the methods
        
    $methods get_class_methods ($d);
        
    sort ($methods);
        foreach (
    $methods as $m)
        {
            echo 
    "<li>$m</li>\n";
        }
        
        echo 
    "</ul></td></tr></table>\n<br />\n";

    Now I'm strugling to use the classes. MY problem is that I have a template xml file with a bunch of empty nodes in, and I need to populate them with other XML data. How would I do this?

    Consider template.xml

    Code:
    <template>
    	<empty></empty>
    	<anotherempty></anotherempty>
    </template>
    How would I add data to <empty> and <anotherempty>?

    If anybody has any links to a some newby tutorials on DOM (PHP5), I'd appreciate it.

    Thanks in advance.

  2. #2
    Back in Action Winged Spider's Avatar
    Join Date
    Jun 2001
    Location
    outside my mind
    Posts
    900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    I can't answer your question because I'm still a php newblet, but awesome little script. Helps me explore PHP 5 a little better.


  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This link (thanks, HarryF), should give you a basic idea of how DOM is used in PHP5.

    http://www.zend.com/php5/articles/php5-xmlphp.php

    JT

  4. #4
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use XPath to get the nodes you want to append to:

    PHP Code:
    $xml = new DomDocument();
    $xml->load(foo.xml);
    $xp = new DomXPath($xml);

    $xp->query('/template/foo')->item(0)->appendChild(new DomElement('bar''contents of bar element')); 
    or if you want to append a seperate DomDocument to the element.

    PHP Code:
    $xml = new DomDocument();
    $xml->load(foo.xml);
    $xml2 = new DomDocument();
    $xml2->load(bar.xml);

    $xp = new DomXPath($xml);

    $node $xp->query('/template/foo')->item(0);

    $node->appendChild($xml->importNode($xml2->documentElementTRUE)); 
    always remember that if you want to copy or move DOM stuff from one document to another you have to import it (using importNode) into the document before you can add it.
    Always open to question or ridicule

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ..use XPath to..
    Of course you would SleepEasy

    K3nny - If you want a better understanding of the DOM in PHP, Harry has a diagram of PHPs DOM over on his website.

    The DOM - PHP4 & PHP5 should be the same, since it's a standard. If the DOM is different in PHP5 then holly christ, what has happened to our standards eh ?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    South Africa
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know this thread is old, but anyway i struggled with the same thing.... came up with this:

    PHP Code:
    <?php

    $dom 
    = new DomDocument('yourxmldoc.xml');

    $dxp = new DomXPath($dom);

    $xp_list $dxp->query('//empty');

    $text_node $dom->CreateTextNode('some data');

    $element $xp_list->item(0);

    $element->appendChild($text_node);

    ?>

  7. #7
    SitePoint Member k3nny's Avatar
    Join Date
    Mar 2004
    Location
    Pretoria
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Hmmm

    Thx everyone for replying, I must admit that I've completely forgotten about this issue and have used some hardcoding techniques... Not too proud, but hey...

    I've been doing .NET development prodominently the last couple of months, and a lot of XML as well. I'll start looking into PHP5 & DOM again later this year and maybe put out a set of small documents on parsing XML with PHP5...

    Thanks again, merry Christmas!

  8. #8
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use the PHP manual for any of the OO stuff in PHP5, coz it's crap (the format they use simply doesn't work for OO)

    Instead, refer to the DOM IDL specs. Do a google search for "DOM IDL".


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
  •