Processing XMLHttpRequest Nodes

S

sboles

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
 
M

Martin Honnen

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

sboles

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
 
M

Martin Honnen

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(responseXML.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.
 
S

sboles

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top