473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Processing XMLHttpRequest Nodes

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

Jul 23 '05 #1
4 4539


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/
Jul 23 '05 #2
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

Jul 23 '05 #3


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/
Jul 23 '05 #4
> 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

Jul 23 '05 #5

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

Similar topics

2
by: Ralf Wahner | last post by:
Dear Masters of XSLT Could I ask you for a clue on the following question? I'd like to use XSLT to transform an XML source file to LaTeX. In the following small example the <para> Element...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
2
by: Unknown User | last post by:
I am trying to implement XMLHttpRequest to a new website, but when I include HTML, the code appears as is, instead of the formated HTML. Please have a look and click the 1st link ("L'Association")...
9
by: William | last post by:
Heya, I have been looking all over Google for ways to *create* an XML document using any Javascript object, and it seems that no one has ever tried or ever needed this. I want to send the...
10
by: shankwheat | last post by:
I'm experimenting with using a AJAX style "processing" icon. The process I'm running in the background with xmlHttp is intensive and takes a 5--10 secs to complete. Instead of my processing icon...
1
by: sanojcp | last post by:
Hi all, am new to php and also javascript. in my program i am using ajax tech and php. when the html loads i am taking data from mysql and create xml. using XMLHttpRequest, i am taking the xml...
1
by: sukil | last post by:
Hi, I have a requirement to generate a form based on various user selections at runtime. So, I am using XMLHttpRequest to call server side programs and passing them parameters that is then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.