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 Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Ugh... I broke my parsing

    Don't ask me how... I don't know. My parsing of the XMLHttpRequest object was working fine Thursday, but as of yesterday it is broken. I loaded up the backup from Thursday and that one isn't working either. But let's set aside my blunder and try to move forward.

    I was parsing an xml response that looks something like this (pared down for easier reading):
    Code:
    <response>
     <target>new</target>
      <data>
       <employee>
         <emp_name>John</emp_name>
         <emp_extension>5555</emp_extension>
       </employee>
       <employee>
         <emp_name>Mary</emp_name>
         <emp_extension>1234</emp_extension>
       </employee>
      </data>
     <error_text>none</error_text>
     <html>
      <head>Success</head>
      <body>
        <p><strong>2</strong> rows returned</p>
      </body>
     </html>
    </response>
    I was able (and am still able) to retrieve the individual values of each node. However, last Thursday, I was grabbing the whole text of the <html> node and using that to display a message to the user. My function would retrieve it as a fully functional html page that I could just write out to a new window. As of yesterday, all my attempts to grab that info only return the visible text sans all tags... "success Success 2 rows returned".

    I would swear that the following code (what I have now) is the same as what I was using last Thursday, but logic would suggest that it isn't... that I altered it between then and now. But here's what I have right now to return a node's value:
    Code:
    function getXmlNodeValue(oXml,strTagName)
    {
        var a = oXml.getElementsByTagName(strTagName);
        if(a==null)
            return null;
        else if(a.length==0)
            return null;
        else
            return a[0].text;
    }
    So basically, I have two needs. First, I need to retrieve a node's value. That seems to work fine. But I also wanted to be able to grab the source text of an entire sub-tree (the <html> node). I had it... lost it... want to find it again

    Thanks in advance,
    Darren

    -------------------
    edit

    Well, I've changed up a bit. I'm now using two functions. One gets values, the other gets the source that I'm after. The first function is almost the same as it was before, but I changed to using .firstChild[0].data instead of .text. It now looks like this:
    Code:
    function getXmlNodeValue(oXml,strTagName)
    {
        var a = oXml.getElementsByTagName(strTagName);
        if(a==null)
            return null;
        else if(a.length==0)
            return null;
        else
            return a[0].firstChild.data;
    }
    I don't like the next function... it's a hack to get what I want. I'm still wondering how I was getting this before I broke things yesterday.

    Code:
    function getXmlNodeSource(strResponseText,strTagName)
    {
        var start = strResponseText.toLowerCase().indexOf('<' + strTagName.toLowerCase() );
        var end = strResponseText.toLowerCase().indexOf('<\/' + strTagName.toLowerCase() + '>');
        return strResponseText.substring(start,end+strTagName.length+3);
     
    }
    Last edited by Darren; 08-15-2007 at 09:26 PM. Reason: progress update

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    You can use 'nodeName' to get its value

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    If your responseText works and the responseXML does not. That tells me there is an error in the XML document.

    Steps I normally have people go through and works is this: http://radio.javaranch.com/pascarell...096122600.html

    I would say skip to the step that says open the xml file up directly in the browser and see if an error message appears.

    I have a feeling you need to use CDATA with that examplecode you have shown.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    Darren (08-20-2007)

  • #4
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Well, Eric, my xml did seem to be the problem. In my message above, I did not include what I was using as my xml header. I was using "Content-type: text/xml" as the information at that link directs, but I was also using <? xml version="1.0" ?> because it was in the example from which I was working. If I update my sample to show that header info I was using, it would be:
    Code:
    Content-type: text/xml
     
    <? xml version="1.0" ?>
    <response>
    <target>new</target>
      <data>
       <employee>
    ... other tags snipped ..
    Based on your hunch that the xml was to blame, and not seeing anything wrong with the content, I took out that <? xml version="1.0" ?> and now it works.



    I still don't have a native replacement for my getXmlNodeSource() function. I would have thought that one of the following would have worked:
    strHtml = oXml.getElementsByTagName("html")[0].innerHTML
    strHtml = oXml.getElementsByTagName("html")[0].innerXML
    strHtml = oXml.getElementsByTagName("html")[0].xml
    None of those work to grab the full tag and all children. If it worked the way I wanted, the value of strHtml would be:
    Code:
     <html>
      <head>Success</head>
      <body>
        <p><strong>2</strong> rows returned</p>
      </body>
    </html>
    Thanks again,
    Darren


  •  

    Posting Permissions

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