Grab all XML of Node (PHP5)

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

    Grab all XML of Node (PHP5)

    I'm looking for a way to grab *all* XML of a DOM node (in PHP5).
    Something like the following, only it doesn't work! Thanks.

    /* I want everything (all XHTML) in <div id="hell=">... </div> */

    $dom = new domdocument;
    @$dom->loadHTMLFile(' http://www.example.com ');
    $xpath = new domxpath($dom);
    $xNodes = $xpath->query("//div[@id='hello']//p/*");
    $sText = '';
    foreach ($xNodes as $xNode)
    {
    $sText .= $xNode->firstChild->data . ' ';
    }

    $sText = strip_tags($sTe xt);
    echo 'Result:' . $sText;


    --
    Google Blogoscoped
    A daily news blog and community covering Google, search, and technology.

  • Jason Kirk

    #2
    Re: Grab all XML of Node (PHP5)

    On Mon, 15 Nov 2004 11:27:55 +0000, Philipp Lenssen wrote:
    [color=blue]
    > I'm looking for a way to grab *all* XML of a DOM node (in PHP5).
    > Something like the following, only it doesn't work! Thanks.[/color]
    {snipped code)

    I prefer to use the saveXML method myself, but you may want to use
    saveHTML.If you just want the entire node subtree then just isolate the
    parent node and save it out, something like

    $text = $xmldom->saveXML($xpa th->query('whateve r')->item(0));

    If you just want the childNodes then I use a function something like this
    (assuming we've already ->loaded something into $xmldom)

    function xpathContents($ xpath,$xmldom)
    #New Xpath Content and query
    $xmlContext = new domXPath($xmldo m);
    $nodeset = $parent = $xmlContext->query($xpath );
    if ( $nodeset->length == 0 ) {
    #Return blank if no results, could always "return FALSE" instead
    return "";
    else {
    $result = "";
    #Select first result from $parent set and then select it's children
    foreach ( $parent->item(0)->childNodes as $child) {
    $result .= $xmldom->saveXML($child )
    }
    return $result;
    }
    }


    Comment

    Working...