Unable To Read XML

C

corydee

Hi All,

I am having some newbie problems with the XML DOM. The XML is returned
in this format:

<weekly_schedule>
<hours>
<day>Sun</day>
<open>1PM</open>
<close>5PM</close>
</hours>
<hours>
<day>Mon</day>
<open>9:30AM</open>
<close>8:30PM</close>
</hours>
</weekly_schedule>

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.

http.responseXML.getElementsByTagName("hours").
[x].getElementsByTagName("day")[0].text

Any ideas why this line is messing things up?

Thanks,
Cory
 
T

Tom Cole

Hi All,

I am having some newbie problems with the XML DOM. The XML is returned
in this format:

<weekly_schedule>
        <hours>
              <day>Sun</day>
              <open>1PM</open>
              <close>5PM</close>
        </hours>
        <hours>
              <day>Mon</day>
              <open>9:30AM</open>
              <close>8:30PM</close>
       </hours>
</weekly_schedule>

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.

http.responseXML.getElementsByTagName("hours").
[x].getElementsByTagName("day")[0].text

Any ideas why this line is messing things up?

Thanks,
Cory

Is this the entire XML being returned? It is missing the <?xml ...?>
tag as it's first line. Also, does your server return a proper content-
type in it's header? You have to set the content-type to "text/xml" or
the XmlHTTPRequest object will not consider it XML, even if it is well-
formed XML.
 
T

Thomas 'PointedEars' Lahn

[...] The XML is returned in this format:

<weekly_schedule>
<hours>
<day>Sun</day>
<open>1PM</open>
<close>5PM</close>
</hours>
[...]
</weekly_schedule>

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.

http.responseXML.getElementsByTagName("hours"). ^
[x].getElementsByTagName("day")[0].text
^^^^

Any ideas why this line is messing things up?

I doubt the above code yields anything anywhere as there is a syntax error.

If we assume that the extra dot is a typo and that `x' is convertible to a
`number' value: An object implementing the Node interface of W3C DOM Level
2+ Core does not have a `text' property. You are looking for the
`textContent' property (DOM3 Core) or the `nodeValue' properties of the
child text nodes (DOM2 Core) instead.


HTH

PointedEars
 
C

corydee

[...] The XML is returned in this format:
<weekly_schedule>
<hours>
<day>Sun</day>
<open>1PM</open>
<close>5PM</close>
</hours>
[...]
</weekly_schedule>

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.
http.responseXML.getElementsByTagName("hours").

^
[x].getElementsByTagName("day")[0].text
^^^^

Any ideas why this line is messing things up?

I doubt the above code yields anything anywhere as there is a syntax error.

If we assume that the extra dot is a typo and that `x' is convertible to a
`number' value: An object implementing the Node interface of W3C DOM Level
2+ Core does not have a `text' property. You are looking for the
`textContent' property (DOM3 Core) or the `nodeValue' properties of the
child text nodes (DOM2 Core) instead.

HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Tom the XML doc is defined with a <?xml version='1.0'
encoding='ISO-8859-1'?> tag at the beginning (apparantly FF strips it
when it displays in a browser window, so my cut and paste didn't pick
it up). The documents headers return (via getAllResponseHeaders()) the
following:
Date: Tue, 08 Apr 2008 11:12:28 GMT
Server: Apache
X-Powered-By: PHP/4.3.6
Keep-Alive: timeout=15, max=95
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/xml

If anyone spots something weird in those headers, please let me know.

Thomas, I think you're on the right track, however, it appears that
the textContent property is recognized by FF(2), but not IE(6 or 7),
where the text property seems to be the opposite. textContent returns
undefined in IE, and nodeValue returns null in both browsers. Is this
an example of having to write browser specific code, or is their a
better way?

Thanks
 
M

Martin Honnen

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.

http.responseXML.getElementsByTagName("hours").
[x].getElementsByTagName("day")[0].text

The W3C DOM does not define a property named 'text' and that is why
Firefox correctly gives you undefined. You might want to use

http.responseXML.getElementsByTagName("hours")[x].getElementsByTagName("day")[0].firstChild.nodeValue
to have cross browser code or you can use
var dayElement =
http.responseXML.getElementsByTagName("hours")[x].getElementsByTagName("day");
var day;
if (typeof dayElement.textContent != 'undefined') {
day = dayElement.textContent;
}
else if (typeof dayElement.text != 'undefined') {
day = dayElement.text;
}
 
C

corydee

I have a XMLHTTPRequest object called http. Using the below line, x is
intended to be in a loop, but even hard coding a zero value doesn't
help. FF always returns undefined, while IE will return the day of the
week as expected.
http.responseXML.getElementsByTagName("hours").
[x].getElementsByTagName("day")[0].text

The W3C DOM does not define a property named 'text' and that is why
Firefox correctly gives you undefined. You might want to use

http.responseXML.getElementsByTagName("hours")[x].getElementsByTagName("day")[0].firstChild.nodeValue
to have cross browser code or you can use
var dayElement =
http.responseXML.getElementsByTagName("hours")[x].getElementsByTagName("day");
var day;
if (typeof dayElement.textContent != 'undefined') {
day = dayElement.textContent;
}
else if (typeof dayElement.text != 'undefined') {
day = dayElement.text;
}

Martin, the firstChild.nodeValue worked perfectly.
Thank you!
 
U

Une Bévue

Martin Honnen said:
The W3C DOM does not define a property named 'text' and that is why
Firefox correctly gives you undefined.

strange (to me), because, in order to put a js source in a <pre /> i
allways use :

document.getElementById('pre_id').appendChild(document.createTextNode(
document.getElementsByTagName('script')[0].text));
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top