DomDocument: setting attributes to a new node

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

    DomDocument: setting attributes to a new node

    Hello,


    I'm trying to create a single function to append new nodes to a
    DOMDocument object:

    public function addElement( $element, $name, $value = '', $attrs = Null
    )
    {
    if( !( $created = $this->createElemen t( $name, $value ) ) )
    throw new Exception( 'Could not create a new node ' );

    if( !( $child = $element->appendChild( $child ) ) )
    throw new Exception( 'Could not append a new node ' );

    // Process and attachs the new attributes from array $attrs if
    any...
    // if( $attrs )
    // foreach( $attrs as $attribute => $value )
    // ...
    }

    So far, so good. Now I want to set attributes and its values of the
    recent created node (playing with createAttribute () and setAttribute(),
    but I can't get the DOMElement from the new child DOMNode. Is it
    possible without issuing getElementsByTa gName() or getElementById( ) ??

    Is another easy way -using DomDocument object- to create a new
    element (such <table width="50%" border="0">...</table>) which has its
    attributes and its values ??

    Thanks in advance,


    Sebastián Araya

  • Janwillem Borleffs

    #2
    Re: DomDocument: setting attributes to a new node

    Sebastian Araya wrote:[color=blue]
    > So far, so good. Now I want to set attributes and its values of the
    > recent created node (playing with createAttribute () and
    > setAttribute(), but I can't get the DOMElement from the new child
    > DOMNode. Is it possible without issuing getElementsByTa gName() or
    > getElementById( ) ??
    >[/color]

    <?php

    class Dom {
    private $dom;

    function __construct() {
    $this->dom = new DomDocument;
    }

    function addElement($nam e, $value = '', $attrs = array()) {
    $element = $this->dom->createElement( $name, $value);
    foreach ($attrs as $a => $v) {
    $element->setAttribute($ a, $v);
    }
    $this->dom->appendChild($e lement);
    }

    function saveXML() {
    return $this->dom->saveXML();
    }
    }

    $dom = new Dom;
    $dom->addElement('ro ot', 'test', array('foo' => 'bar'));
    print htmlentities($d om->saveXML());

    ?>
    [color=blue]
    > Is another easy way -using DomDocument object- to create a new
    > element (such <table width="50%" border="0">...</table>) which has its
    > attributes and its values ??
    >[/color]

    You could use the following (although not very pretty):

    <?php

    class Dom {
    private $dom;

    function __construct() {
    $this->dom = new DomDocument;
    }

    function addElement($ele ment) {
    $dom = DomDocument::lo adXML($element) ;
    $node = $this->dom->importNode(
    $dom->documentElemen t,
    true
    );
    $this->dom->appendChild($n ode);
    }

    function saveXML() {
    return $this->dom->saveXML();
    }
    }

    $dom = new Dom;
    $dom->addElement('<r oot foo="bar">test</root>');
    print htmlentities($d om->saveXML());

    ?>


    JW



    Comment

    • Sebastian  Araya

      #3
      Re: DomDocument: setting attributes to a new node

      Brillant.Thanks JW!

      Comment

      Working...