Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Processing XMLHttpRequest Nodes

Reply
Thread Tools

Processing XMLHttpRequest Nodes

 
 
sboles
Guest
Posts: n/a
 
      04-09-2005
Hello,

I am trying to process each node from XMLHttpRequest.responseXML and
set the node's 'className' property based on the value of the 'class'
attribute. I have written a recursive function to do this. However,
after the function completes, the node's className properties are not
set. I was under the impression that objects were passed to functions
as references. What have I missed?

Code:

// grab the container node
var node = xmlReq.responseXML.getElementsByTagName('div')[0];

// process node and children
process_node(node);

function process_node(n) {
if ( n.nodeType == 1 ) {
if ( n.getAttribute('class') ) {
n.className = n.getAttribute('class');
alert('set className: '+n.className);
}
for ( var c=n.firstChild; c!=null; c=c.nextSibling ) {
process_node(c);
}
}
}

The XML response text looks like this:

<div class="menu_container">
<div class="menu_closed">Menu Text 1</div>
<div class="menu_closed">Menu Text 2</div>
</div>

Thanks!
shawn

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-09-2005


sboles wrote:


> I am trying to process each node from XMLHttpRequest.responseXML and
> set the node's 'className' property based on the value of the 'class'
> attribute. I have written a recursive function to do this. However,
> after the function completes, the node's className properties are not
> set. I was under the impression that objects were passed to functions
> as references. What have I missed?
>
> Code:
>
> // grab the container node
> var node = xmlReq.responseXML.getElementsByTagName('div')[0];
>
> // process node and children
> process_node(node);
>
> function process_node(n) {
> if ( n.nodeType == 1 ) {
> if ( n.getAttribute('class') ) {
> n.className = n.getAttribute('class');
> alert('set className: '+n.className);


Is that alert where you control whether setting the property with name
className fails?
If you are using IE with MSXML then I would expect the line
n.className = n.getAttribute('class');
to throw an error that the property is not supported.
For Mozilla I don't see any problem, I just wonder what it is good for
to try to set a className property on XML nodes. What do you want to
achieve with that?


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
sboles
Guest
Posts: n/a
 
      04-10-2005
Martin,

Thank you for your response.

> Is that alert where you control whether setting the
> property with name className faisl?


I use the alert to verify that the className property is set. The
alert suggests that the property does get set within the function.
Outside of the function, the property is empty.

> If you are using IE


I am using Mozilla. I should have mentioned that in my earlier post.
I am aware that IE does not support the className property.

> I just wonder what it is good for to try to set a className
> property on XML nodes. What do you want to achieve with that?


My specific application: I am transforming XML data into HTML and then
injecting it into the page's DOM. The problem I am having is that the
class attribute is not handled as a class property. The result then is
that the CSS style definition for the class is not applied by the
browser. My general problem is: How to inject xmlResponse data into
the page's DOM and preserve the class name and id properties?

Thanks again for your reply,
shawn

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      04-10-2005


sboles wrote:


> My specific application: I am transforming XML data into HTML and then
> injecting it into the page's DOM. The problem I am having is that the
> class attribute is not handled as a class property. The result then is
> that the CSS style definition for the class is not applied by the
> browser. My general problem is: How to inject xmlResponse data into
> the page's DOM and preserve the class name and id properties?


If you want to have HTML elements in responseXML then you need to make
sure you send XHTML with the proper namespace e.g. instead of sending
<p class="style1">Kibology for all.</p>
make sure you send
<p xmlns="http://www.w3.org/1999/xhtml" class="style1">Kibology for
all.</p>
then when Mozilla's XML parser parses the response sent from the server
into responseXML it has (X)HTML DOM nodes in there which you can
directly import into your existing HTML DOM document e.g. if responseXML
is an XML document with the above markup then you can do

document.body.appendChild(document.importNode(resp onseXML.documentElement,
true));

No need to try to fix up any properties, that will not work, if you have
<p class="style1">...</p>
parsed by an XML parser then it doesn't create an HTMLPElement which has
a className property but simply creates an Element node with tag name
'p' and one attribute named 'class' with value 'style1' but neither the
element is treated as HTML <p> element node nor the attribute as
anything related to CSS style classes.

So use the proper namespace, then import nodes and Mozilla will do the rest.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
sboles
Guest
Posts: n/a
 
      04-11-2005
> If you want to have HTML elements in
> responseXML then you need to make
> sure you send XHTML with the proper
> namespace


Ok, this makes sense. I figured that I was missing something
fundamental.

Thanks again,
shawn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
finding nodes that don't match other nodes mlybarger@gmail.com XML 2 01-27-2005 07:26 PM
Looking A Nodes From Within Nodes Johnny Ooi XML 10 11-14-2004 06:55 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments