Processing XMLHttpRequest Nodes

Discussion in 'Javascript' started by sboles, Apr 9, 2005.

  1. sboles

    sboles Guest

    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
    sboles, Apr 9, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Apr 9, 2005
    #2
    1. Advertising

  3. sboles

    sboles Guest

    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
    sboles, Apr 10, 2005
    #3
  4. 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 10, 2005
    #4
  5. sboles

    sboles Guest

    > 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
    sboles, Apr 11, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    437
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    519
    gavnosis
    Aug 2, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    402
    Patrick TJ McPhee
    Jun 17, 2004
  4. Johnny Ooi

    Looking A Nodes From Within Nodes

    Johnny Ooi, Nov 13, 2004, in forum: XML
    Replies:
    10
    Views:
    653
    Johnny Ooi
    Nov 14, 2004
  5. Replies:
    2
    Views:
    395
Loading...

Share This Page