appendData() DOM

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

    appendData() DOM

    Hello everyone. I've been looking for examples on this:

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    but can't really find one. I've got a function that generates a return value
    in HTML, and need to append that to the body tag of my document. So I've
    captured the contents of the page with output buffering, and have loaded it
    into the PHP DOM. I've isolated the body element:

    ,----[ php ]
    $dom = new DomDocument;
    $dom->LoadHTML($buff er);
    $body = $dom->getElementsByT agName('body');
    $body = $body->item(0);
    `----

    And want to do the following:

    ,----[ php ]
    $body->appendData($ht ml);
    `----


    Of course as the appendData method is a member of the DOMCharacterDat a
    class, I get an error:

    Fatal error: Call to undefined method DOMElement::app endData()

    But how can I access the appendData() method to get the results I want?

    Thanks everyone,

    James

    ps. This is my first post to a newsgroup... Sorry if I'm posting this
    improperly or something.
  • Janwillem Borleffs

    #2
    Re: appendData() DOM

    James wrote:[color=blue]
    > Hello everyone. I've been looking for examples on this:
    >
    > http://www.php.net/manual/en/functi...-appenddata.php
    >
    > but can't really find one. I've got a function that generates a
    > return value in HTML, and need to append that to the body tag of my
    > document. So I've captured the contents of the page with output
    > buffering, and have loaded it into the PHP DOM. I've isolated the
    > body element:
    >[/color]

    CDATA is data encapsuled between '<![CDATA[' and ']]>' markers. The
    advantage of CDATA sections is that you can put anything in between,
    including invalid xml and/or characters as these sections are ignored by the
    parser.

    But what you probably need is just to append a value to the body tag's node
    value, which is done as follows:

    $buffer = '<html><body>bl a</body></html>';
    $dom = new DomDocument;
    $dom->LoadHTML($buff er);
    $body = $dom->getElementsByT agName('body');
    $body->item(0)->nodeValue .= ' bla';

    // Prints: <html><body>b la bla</body></html>
    print $dom->saveHTML();
    [color=blue]
    > ps. This is my first post to a newsgroup... Sorry if I'm posting this
    > improperly or something.[/color]

    Posting is fine and even better then from some 'experienced' posters.


    JW



    Comment

    • James

      #3
      Re: appendData() DOM

      Janwillem Borleffs wrote:
      [color=blue]
      > CDATA is data encapsuled between '<![CDATA[' and ']]>' markers. The
      > advantage of CDATA sections is that you can put anything in between,
      > including invalid xml and/or characters as these sections are ignored by
      > the parser.[/color]

      Perect, this is just what I was looking for. Thank you!
      [color=blue]
      >
      > $buffer = '<html><body>bl a</body></html>';
      > $dom = new DomDocument;
      > $dom->LoadHTML($buff er);
      > $body = $dom->getElementsByT agName('body');
      > $body->item(0)->nodeValue .= ' bla';
      >
      > // Prints: <html><body>b la bla</body></html>
      > print $dom->saveHTML();
      >[/color]

      I was actually really trying to do something very similar to what you've got
      above. My issue was that when I set

      $body->item(0)->nodeValue .= '<b>blah</b>';

      The tags would get a treatment with htmlentities(). I tried turning that
      behavior off:

      $dom->substituteEnti ties = false;

      But that didn't really do what I wanted. The CDATA markers do exactly what I
      need.
      [color=blue]
      > Posting is fine and even better then from some 'experienced' posters.[/color]

      Flattered :)

      Thanks again,
      James

      Comment

      Working...