Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parsing xml in JS - deleting whitespace in mozilla

    I'm writing a JavaScript that loads an XML file, parses the info, and pushes it into an array. The script needs to work in IE and and Firefox.Loading the XML file is easy enough.Parsing the file in IE is pretty simple as well as is pushing the info into an array.

    The problem occurs when in the function I'm using to remove the whitespace from the xml file for firefox or netscape.

    Here is the function I'm using. It's called parseXml()

    1.) function parseXml() {
    2.) var msgObj = xmlDoc.getElementsByTagName("messages")[0];
    3.) for (i=0;i<msgObj.childNodes.length - 1;i++) {
    4.) if ((msgObj.childNodes[i].nodeType == 3) && (!notWhitespace.test(msgObj.childNodes[i].nodeValue))) {
    msgObj.removeChild(msgObj.childNodes[i]);
    5.)
    // i--;
    6.) }
    7.) photoArray.push(msgObj.childNodes[i].firstChild.nodeValue);
    8.) }
    9.) }

    Here is the content of the xml file I'm testing with - daily.xml
    <?xml version="1.0" encoding="iso-8859-1"?>
    <messages>
    <daily>Today is Sunday.</daily>
    <daily>Today is Monday.</daily>
    <daily>Today is Tuesday.</daily>
    <daily>Today is Wednesday.</daily>
    <daily>Today is Thursday.</daily>
    <daily>Today is Friday.</daily>
    <daily>Today is Saturday.</daily>
    </messages>


    The script loads the xml file just fine in both IE and Firefox.

    The problem occurs w/in parseXml(); As it is it works great in Firefox. But with IE it leaves out the info - Today is Saturday - the last daily tag.

    It does this because of the "-1" in line 3:
    for (i=0;i<msgObj.childNodes.length - 1;i++)

    Apparently the line should read:
    for (i=0;i<msgObj.childNodes.length;i++)
    and line 5 should be used rather than commented out. But if i do this - firefox generates an error from line 7 - I believe it has something to do with the last value of i in the for loop being a whitespace.

    Any ideas on this. I've the html doc and the xml doc as txt files in case that helps out.
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    why not just say
    Code:
    xmlDoc.getElementsByTagName("messages")[0].getElementsByTagName("daily")
    or if you have access to it before its node-ified, this regexp will kill all whitespace
    Code:
    stringref.replace(/\s/g,"");
    Last edited by subhailc; 06-24-2006 at 09:56 AM.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    when this occurs

    msgObj.removeChild(msgObj.childNodes[i]);

    then the number of child nodes decreases and so does the count and the number of child nodes

    tis necessary to first load the child nodes into an array and loop this array to remove the child nodes from their parent
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Vic,
    That worked perfectly. Thanks so much.
    Charles


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •