Holzner's book

S

steve.cpp

Hi,
I had a question about an example in Holzner's "XML: A Beginner's
Guide." I have to learn XML quickly and picked up Holzner's book
based on some colleagues advice, as well as on some online reviews. I
have minimal experience with XML, or with JavaScript, so apologies if
this is too easy.

He has the following code posted as an example:

<html>
<head>
<title>
Retrieving data from an XML document.
</title>
<xml id="firstXML" src="hello.xml"></xml>
<script language="JavaScript">
function getData()
{
xmldoc = document.all("firstXML").XMLDocument;

nodeDoc = xmldoc.documentElement;
nodeGreeting = nodeDoc.firstChild;

outputMessage = "Greeting: " +
nodeGreeting.firstChild.nodeValue;
message.innerHTML=outputMessage;
}
</script>
</head>

<body>
<center>
<h1>
Retrieving data from an XML document.
</h1>

<div id="message"></DIV>
<p>
<input type="button" value="Read the greeting."
onclick="getData()">
</center>
</body>
</html>

When I enter this same code and load the page using Firefox on a Linux
machine, I click the "Read the greeting button, nothing appears in the
web page, when "Greeting: Hello from XML" should appear.

When I open the error console and try the button again, it indicates
that xmldoc is undefined:

xmldoc = document.all("firstXML").XMLDocument;

I don't have any JavaScript books and the local Barnes & Noble didn't
have any in stock so I did some quick searching and found that I
should use "getElementById" instead.

I tried that and had the same results:

xmldoc = document.getElementById
("firstXML").XMLDocument;

I tried a few other things but again the same result. I tried the
original code on a Windows box using IE and the code works as
described.

Is there a workaround to getting xmldoc defined correctly with Firefox?
 
J

Johannes Koch

Joe said:
I don't think the <xml> tag is standard HTML...

It's a proprietary thing by Microsoft.

The XML spec reads ( said:
Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification.

So an element type xml (<xml id="firstXML" src="hello.xml"></xml>) is
not allowed in XML-based languages. The code quoted by the OP, however,
looks like some HTMLish tag soup. But, well...
 
M

Martin Honnen

steve.cpp said:
<html>
<head>
<title>
Retrieving data from an XML document.
</title>
<xml id="firstXML" src="hello.xml"></xml>

I tried a few other things but again the same result. I tried the
original code on a Windows box using IE and the code works as
described.

Is there a workaround to getting xmldoc defined correctly with Firefox?

There is no element named 'xml' in HTML 4 or XHTML 1. Only IE/Windows
supports such an element for so called XML data islands.

With other browsers if you want to use XML data you can use JavaScript
and XMLHttpRequest to load the data. Since IE 7 IE also supports that,
with earlier versions you can use an ActiveXObject.

https://developer.mozilla.org/En/Using_XMLHttpRequest

That way you can request application/xml or text/xml content and the
XMLHttpRequest object loads and parses that and exposes a responseXML
property with an XML DOM document.
 
J

Joe Kesselman

Johannes said:
So an element type xml (<xml id="firstXML" src="hello.xml"></xml>) is
not allowed in XML-based languages.

HTML is not an XML-based language (at least not until you get to XHTML).

But if you're using something proprietary, I can't help.
 
P

Peter Flynn

steve.cpp said:
Hi,
I had a question about an example in Holzner's "XML: A Beginner's
Guide." I have to learn XML quickly and picked up Holzner's book
based on some colleagues advice, as well as on some online reviews. I
have minimal experience with XML, or with JavaScript, so apologies if
this is too easy.

He has the following code posted as an example:

<html>
<head>
<title>
Retrieving data from an XML document.
</title>
<xml id="firstXML" src="hello.xml"></xml>
<script language="JavaScript">
function getData()
{
xmldoc = document.all("firstXML").XMLDocument;

nodeDoc = xmldoc.documentElement;
nodeGreeting = nodeDoc.firstChild;

outputMessage = "Greeting: " +
nodeGreeting.firstChild.nodeValue;
message.innerHTML=outputMessage;
}
</script>
</head>

<body>
<center>
<h1>
Retrieving data from an XML document.
</h1>

<div id="message"></DIV>
<p>
<input type="button" value="Read the greeting."
onclick="getData()">
</center>
</body>
</html>

Any book that gives this as an "example" of HTML should probably be set
aside. It's neither HTML nor XHTML, and it shows signs of having been
constructed by someone unfamiliar with the conventions of markup. The
fact that it might work in IE and nowhere else is likely to be
sufficient reason to discard it.

///Peter
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top