Processing XMLHttpRequest Nodes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sboles

    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.response XML.getElements ByTagName('div' )[0];

    // process node and children
    process_node(no de);

    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_con tainer">
    <div class="menu_clo sed">Menu Text 1</div>
    <div class="menu_clo sed">Menu Text 2</div>
    </div>

    Thanks!
    shawn

  • Martin Honnen

    #2
    Re: Processing XMLHttpRequest Nodes



    sboles wrote:

    [color=blue]
    > 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.response XML.getElements ByTagName('div' )[0];
    >
    > // process node and children
    > process_node(no de);
    >
    > function process_node(n) {
    > if ( n.nodeType == 1 ) {
    > if ( n.getAttribute( 'class') ) {
    > n.className = n.getAttribute( 'class');
    > alert('set className: '+n.className);[/color]

    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

    Comment

    • sboles

      #3
      Re: Processing XMLHttpRequest Nodes

      Martin,

      Thank you for your response.
      [color=blue]
      > Is that alert where you control whether setting the
      > property with name className faisl?[/color]

      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.
      [color=blue]
      > If you are using IE[/color]

      I am using Mozilla. I should have mentioned that in my earlier post.
      I am aware that IE does not support the className property.
      [color=blue]
      > 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?[/color]

      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

      Comment

      • Martin Honnen

        #4
        Re: Processing XMLHttpRequest Nodes



        sboles wrote:

        [color=blue]
        > 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?[/color]

        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.a ppendChild(docu ment.importNode (responseXML.do cumentElement,
        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

        Comment

        • sboles

          #5
          Re: Processing XMLHttpRequest Nodes

          > If you want to have HTML elements in[color=blue]
          > responseXML then you need to make
          > sure you send XHTML with the proper
          > namespace[/color]

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

          Thanks again,
          shawn

          Comment

          Working...