...

Ugh... I broke my parsing

Darren
08-15-2007, 08:28 PM
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):
<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:
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:
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.

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);

}

rwedge
08-15-2007, 10:12 PM
You can use 'nodeName' to get its value

A1ien51
08-16-2007, 09:41 PM
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/pascarello/2006/09/12/1158096122600.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

Darren
08-20-2007, 03:44 PM
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: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].innerHTMLstrHtml = oXml.getElementsByTagName("html")[0].innerXMLstrHtml = oXml.getElementsByTagName("html")[0].xmlNone of those work to grab the full tag and all children. If it worked the way I wanted, the value of strHtml would be:
<html>
<head>Success</head>
<body>
<p><strong>2</strong> rows returned</p>
</body>
</html>Thanks again,
Darren



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum