Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Request help parsing XML reply from HTTPRequest

Reply
Thread Tools

Request help parsing XML reply from HTTPRequest

 
 
Doug Miller
Guest
Posts: n/a
 
      09-26-2007
I can't seem to get this reply parsed correctly. All I'm really interested in
are Package.Postage.MailService and Package.Postage.Rate. Code snippet
follows.

<?xml version="1.0" ?>
<RateV3Response>
<Package ID="1">
<ZipOrigination>46228</ZipOrigination>
<ZipDestination>62522</ZipDestination>
<Pounds>3</Pounds>
<Ounces>0</Ounces>
<Size>REGULAR</Size>
<Zone>3</Zone>
<Postage CLASSID="1">
<MailService>Priority Mail</MailService>
<Rate>5.70</Rate>
</Postage>
</Package>
</RateV3Response>

I'm encountering *no* problems sending the request, or receiving the reply --
or displaying the *entire* reply. It's getting at the individual fields that
I'm having trouble with.

Here's the relevant portion of the code. Somebody please show me what I'm
doing wrong (or failing to do, as the case may be).

TIA...

var package = new Array;
package = objSrvHTTP.responseXML.getElementsByTagName("Packa ge");

alert (package.length);
// displays 0 !! What's going on? Should be 1, shouldn't it?

alert (package.firstChild.nodeValue);
// complains that the object doesn't support this property or method

for (var i = 0; i < package.length; i++) {
// since package.length = 0, obviously the loop will do nothing

var pkg = package.item(i);
var postage = pkg.getElementsByTagName("Postage");

for (var j = 0; j < postage.length; j++) {
var pstg = postage.item(j);
var rate = pstg.getElementsByTagName("Rate");
var srvc = pstg.getElementsByTagName("MailService");
Service.innerText = srvc.item(0).firstChild.nodeValue;
Rate.innerText = rate.item(0).firstChild.nodeValue;
}
}


--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-26-2007
Doug Miller wrote:
> I can't seem to get this reply parsed correctly. All I'm really interested in
> are Package.Postage.MailService and Package.Postage.Rate. Code snippet
> follows.
>
> <?xml version="1.0" ?>
> <RateV3Response>
> <Package ID="1">
> <ZipOrigination>46228</ZipOrigination>
> <ZipDestination>62522</ZipDestination>
> <Pounds>3</Pounds>
> <Ounces>0</Ounces>
> <Size>REGULAR</Size>
> <Zone>3</Zone>
> <Postage CLASSID="1">
> <MailService>Priority Mail</MailService>
> <Rate>5.70</Rate>
> </Postage>
> </Package>
> </RateV3Response>
>
> I'm encountering *no* problems sending the request, or receiving the reply --
> or displaying the *entire* reply.


If you have no trouble displaying the entire reply, it is possible that you
are serving it with a wrong media type declaration (Content-Type header).

> It's getting at the individual fields that I'm having trouble with.
>
> Here's the relevant portion of the code. Somebody please show me what I'm
> doing wrong (or failing to do, as the case may be).
>
> TIA...
>
> var package = new Array;
> package = objSrvHTTP.responseXML.getElementsByTagName("Packa ge");


Initialization with an Array object reference is futile if it is overwritten
shortly after. The language you are using is loosely-typed.

It makes especially no sense because the value returned from
Document::getElementsByTagName() is a reference to a NodeList object, not

http://www.w3.org/TR/DOM-Level-2-Cor...tml#ID-A6C9094

> alert (package.length);
> // displays 0 !! What's going on? Should be 1, shouldn't it?


Probably.

> alert (package.firstChild.nodeValue);
> // complains that the object doesn't support this property or method


Which would mean that `package' does not not refer to a Node object for some
reason. Debug that.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-26-2007
Doug Miller wrote:
> [...] Thomas 'PointedEars' Lahn [...]wrote:
>> Doug Miller wrote:
>>> I can't seem to get this reply parsed correctly. All I'm really interested in
>>> are Package.Postage.MailService and Package.Postage.Rate. Code snippet
>>> follows.
>>>
>>> <?xml version="1.0" ?>
>>> <RateV3Response>
>>> <Package ID="1">
>>> [...]
>>> </Package>
>>> </RateV3Response>
>>>
>>> I'm encountering *no* problems sending the request, or receiving the reply --
>>> or displaying the *entire* reply.

>
> Let me clarify:
>
> Displaying objSrvHTTP.responseText works just fine; unfortunately, that's not

^^^^^^^^^^^^
> much use to me, as I need to extract certain individual elements.
>
> All my attempts to parse objSrvHTTP.responseXML are so far unsuccessful.


Hence my educated guess that you are serving it with the wrong media type.

>> If you have no trouble displaying the entire reply, it is possible that you
>> are serving it with a wrong media type declaration (Content-Type header).

>
> I'm not sure what you're getting at here. I have no control over the format of
> the reply; I have to deal with it as it is. Can you explain a little further,
> please?


You have to serve the response declared with an XML media (MIME) type in its
Content-Type header (no matter the format of its message body), or the
`responseXML' property will most certainly not work. `text/xml' is the
media type that should be declared here.

>>> It's getting at the individual fields that I'm having trouble with.
>>> [...]
>>>
>>> var package = new Array;
>>> package = objSrvHTTP.responseXML.getElementsByTagName("Packa ge");

>> Initialization with an Array object reference is futile if it is overwritten
>> shortly after. The language you are using is loosely-typed.

>
> OK, so if I understand you correctly, this could/should be replaced with
>
> var package = objSrvHTTP.responseXML <etc>


Exactly, though more feature tests at runtime should be performed. See the
FAQ Notes for that.

> Tried that - no difference.


Then it is even more likely that it is the wrong media type.

>>> alert (package.length);
>>> // displays 0 !! What's going on? Should be 1, shouldn't it?

>> Probably.
>>
>>> alert (package.firstChild.nodeValue);
>>> // complains that the object doesn't support this property or method

>> Which would mean that `package' does not not refer to a Node object for some
>> reason. Debug that.

>
> Sorry, I don't follow you there at all. If getElementsByTagName() returns a
> Node object, and that return value is assigned to package, how can package
> *not* refer to a Node object?


It can not refer to a Node object under certain circumstances. You should
use a debugger (or similar means such as `window.alert(package)' etc.) to
find out there is such a situation here.

You have provided too little information about your server-side and
client-side environment to allow for more educated guesses, let alone
for providing an informed solution to the problem.


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
 
Reply With Quote
 
Doug Miller
Guest
Posts: n/a
 
      09-26-2007
In article <(E-Mail Removed)>, Thomas 'PointedEars' Lahn <(E-Mail Removed)> wrote:
>You have to serve the response declared with an XML media (MIME) type in its
>Content-Type header (no matter the format of its message body), or the
>`responseXML' property will most certainly not work. `text/xml' is the
>media type that should be declared here.


I guess this is where I'm really lost: How can I "serve the response declared
with an XML media type in its Content-Type header" when I have no control over
what comes back from the server?

[...]
>You have provided too little information about your server-side and
>client-side environment to allow for more educated guesses, let alone
>for providing an informed solution to the problem.


Client side is IE6 / Win XP Pro SP2. I have no idea what the server side
environment is. It doesn't belong to me, I have no control over it, and I have
to take what I can get.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      09-26-2007
Doug Miller meinte:

> I guess this is where I'm really lost: How can I "serve the response declared
> with an XML media type in its Content-Type header" when I have no control over
> what comes back from the server?


You could at least check it (and perhaps rule out a possible source of
your error). Get some proper debugging tools, which will display the
response's header.

> Client side is IE6 / Win XP Pro SP2. I have no idea what the server side
> environment is. It doesn't belong to me, I have no control over it, and I have
> to take what I can get.


Get Firefox and Firebug and then start debugging.

Gregor



--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-26-2007
Doug Miller wrote:
> In article <(E-Mail Removed)>, Thomas 'PointedEars' Lahn <(E-Mail Removed)> wrote:
>> You have to serve the response declared with an XML media (MIME) type in its
>> Content-Type header (no matter the format of its message body), or the
>> `responseXML' property will most certainly not work. `text/xml' is the
>> media type that should be declared here.

>
> I guess this is where I'm really lost: How can I "serve the response declared
> with an XML media type in its Content-Type header" when I have no control over
> what comes back from the server?


Ask the person that is administrating the server to serve the document with
a proper media type so that you can make use of it.

> [...]
>> You have provided too little information about your server-side and
>> client-side environment to allow for more educated guesses, let alone
>> for providing an informed solution to the problem.

>
> Client side is IE6 / Win XP Pro SP2. I have no idea what the server side
> environment is. It doesn't belong to me, I have no control over it, and
> I have to take what I can get.


Another possibility is to use a parser object to parse the XML markup into
an XML document (object):

http://www.faqts.com/knowledge_base/...html/aid/15302


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      09-26-2007
Doug Miller wrote:

> Client side is IE6 / Win XP Pro SP2. I have no idea what the server side
> environment is. It doesn't belong to me, I have no control over it, and I have
> to take what I can get.


Do you simply want to load an XML document and parse it with IE 6? Then
you don't need XMLHttpRequest, instead you can do e.g.
var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDocument.onreadystatechange = function () {
if (xmlDocument.readyState === 4) {
alert(xmlDocument.getElementsByTagName('package'). length);
}
};
xmlDocument.load('file.xml');

XMLHTTP and responseXML will only work if the server sends the XML with
the HTTP response header Content-Type with value text/xml or
application/xml.
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Doug Miller
Guest
Posts: n/a
 
      09-26-2007
In article <(E-Mail Removed)>, Thomas 'PointedEars' Lahn <(E-Mail Removed)> wrote:

>Ask the person that is administrating the server to serve the document with
>a proper media type so that you can make use of it.


Unfortunately that is not an option.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
 
Reply With Quote
 
Doug Miller
Guest
Posts: n/a
 
      09-26-2007
In article <46fa816b$0$16109$(E-Mail Removed)-online.net>, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>Doug Miller wrote:
>
>> Client side is IE6 / Win XP Pro SP2. I have no idea what the server side
>> environment is. It doesn't belong to me, I have no control over it, and I

> have
>> to take what I can get.

>
>Do you simply want to load an XML document and parse it with IE 6?


No -- I'm sending an HTTP request to a remote server, and I need to parse the
XML response that the server returns.

>Then you don't need XMLHttpRequest, instead you can do e.g.
> var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
> xmlDocument.onreadystatechange = function () {
> if (xmlDocument.readyState === 4) {
> alert(xmlDocument.getElementsByTagName('package'). length);
> }
> };
> xmlDocument.load('file.xml');
>
>XMLHTTP and responseXML will only work if the server sends the XML with
>the HTTP response header Content-Type with value text/xml or
>application/xml.


And if it doesn't?

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-26-2007
Doug Miller wrote:
> In article <(E-Mail Removed)>, Thomas 'PointedEars' Lahn <(E-Mail Removed)> wrote:
>> Ask the person that is administrating the server to serve the document with
>> a proper media type so that you can make use of it.

>
> Unfortunately that is not an option.


Why that would be so is beyond me; you could at least try to convince them
to improve the quality of their service. Unless you are using their service
without authorization, of course.

Anyway, if you cannot manage a change in the headers, you have to pick the
second option that I proposed. A possible issue of copyright/author's
rights remained, though.


PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to reply to posts on google groups (and not reply to just author) davidj411 Python 1 05-27-2008 04:13 PM
Reply....Reply All not in Browser Denny B Computer Support 3 04-24-2006 01:59 PM
can sombody tell me how to reply to sombody elses message and include the original message in the reply? Computer Support 3 08-24-2003 12:58 PM
Re: can sombody tell me how to reply to sombody elses message and include the original message in the reply? Computer Support 0 07-24-2003 09:21 PM



Advertisments