473,804 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 22 '05 #1
2 4264
Sebastian Araya wrote:
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( ) ??

<?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());

?>
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 ??


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

Nov 22 '05 #2
Brillant.Thanks JW!

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5008
by: bird | last post by:
?sitem <?xml version="1.0"?> <RootNode> ' one root node <Transaction> ' doc element node <AccountNumber>1001000</AccountNumber> ' node list/children <Amount>100.00</Amount> </Transaction> </RootNode>
1
11194
by: relaxedrob | last post by:
Howdy All! I am really stuck with this one - I want to completely create a table within JavaScript and insert it into the document, with onMouseOut and onMouseOver handlers in the table rows. Below is a sample of the code I have created. It all works in Netscape 7.1, but in IE 6 it shows the table but the handlers do not run. I can prove the handlers are even there (see the commented out alert command in the code) so why aren't they...
3
17347
by: Rinchenzo-La | last post by:
Hey there I've got a little stuck with the XML DOM in jscript (only just learning jscript so this might be a bit of a dumb question!) I'm using jscript inside an HTML page to open an XML DOMDocument object, do stuff to the xml then I'd really like to save the changes (have done this a million times in VB apps) - which is where the problem comes in....
21
4007
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
2
1772
by: RichardHatcher.com | last post by:
I am writing C code using xpath to extract some data from a XML document. The file uses namespaces, but the name spaces are all listed in the root node of the file as attributes. I want to extract all the attributes, so that I can register the namespaces. The document looks like: <?xml version="1.0" encoding="UTF-8" ?> <ConfigDataFile xmlns:SRI="SRI" xmlns:SIW="SIW" xmlns:QBQ="QBQ" xmlns:VLD="VLD"...
0
5127
by: paulroskilly | last post by:
Hi, Im using the DomDocument class in PHP5 to parse thru a xml file, but it keeps timing out on the load method : $dom = new DomDocument(); $dom->load("<some url>"); Browsing to the url in a browser does load, although it takes about 11 minutes, its a big amount of data.
0
1392
by: yawnmoth | last post by:
First, here's my code: <?php $xml = new DOMDocument(); $xml->preserveWhiteSpace = true; $xml->loadXML(' <html> <head/> <body/>
2
4320
by: rds80 | last post by:
In the xml document below, I would like to retrieve the distinct attributes for the element '<Bal>'. However, I haven't had any success. Here is what I have so far: <TRANS> <TRAN TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">
12
18328
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the existing node with the new one. For example first xml file will be like; <Root> <Node Id ="1" FormId="form1" Value="value1"/> <Node Id ="2" FormId="form1" Value="value2"/> <Node Id ="3" FormId="form1" Value="value3"/>...
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.