| Author |
Rough Start
|
Meadowlark Bradsher
Ranch Hand
Joined: Jan 23, 2001
Posts: 109
|
|
Hello good folks, I just bought Inside XML, kind of on Ajith's "kind-of" recommendation, to help me prepare for IBM's XML certification (see http://www.javaranch.com/ubb/Forum31/HTML/000367.html ). Well the first programming assignment, a 15 minute IE 5+ javascript loading and reading of a simple XML document, is returning a null object instead of the root element. This is the HTML & Jscript: <HTML> <HEAD> <TITLE> Reading XML element values </TITLE> <SCRIPT LANGUAGE="JavaScript"> function readXMLDocument() { var xmldoc, meetingsNode, meetingNode, peopleNode var first_nameNode, last_nameNode, outputText xmldoc = new ActiveXObject("Microsoft.XMLDOM") xmldoc.load("meetings.xml") meetingsNode = xmldoc.documentElement meetingNode = meetingsNode.firstChild peopleNode = meetingNode.lastChild personNode = peopleNode.lastChild first_nameNode = personNode.firstChild last_nameNode = first_nameNode.nextSibling outputText = "Third name: " + first_nameNode.firstChild.nodeValue + ' ' + last_nameNode.firstChild.nodeValue messageDIV.innerHTML=outputText } </SCRIPT> </HEAD> <BODY> <CENTER> <H1> Reading XML element values </H1> <INPUT TYPE="BUTTON" VALUE="Get the name of the third person" ONCLICK="readXMLDocument()"> <P> <DIV ID="messageDIV"></DIV> </CENTER> </BODY> </HTML> and "meetings.xml" is as follows: <?xml version="1.0"?> <MEETINGS> <MEETING TYPE="informal"> <MEETING_TITLE>XML In The Real World</MEETING_TITLE> <MEETING_NUMBER>2079</MEETING_NUMBER> <SUBJECT>XML</SUBJECT> <DATE>6/1/2002</DATE> <PEOPLE> <PERSON ATTENDENCE="present"> <FIRST_NAME>Edward</FIRST_NAME> <LAST_NAME>Samson</LAST_NAME> </PERSON> <PERSON ATTENDENCE="absent"> <FIRST_NAME>Ernestine</FIRST_NAME> <LAST_NAME>Johnson</LAST_NAME> </PERSON> <PERSON ATTENDENCE="present"> <FIRST_NAME>Betty</FIRST_NAME> <LAST_NAME>Richardson</LAST_NAME> </PERSON> </PEOPLE> </MEETING> </MEETINGS> On the first document "meetings.xml" is in the same directory but "meetingsNode = xmldoc.documentElement" assigns meetingsNode to null and the following line thus creates an error. My best guess is that either the book has an error not mentioned in the errata or I am missing something fundamental and assumed. Does anyone have any insights? Meadowlark Bradsher
|
Meadowlark Bradsher
SCJ2P, IBM XML V1, Series 7/63
|
 |
Meadowlark Bradsher
Ranch Hand
Joined: Jan 23, 2001
Posts: 109
|
|
Originally posted by Meadowlark Bradsher: This is the HTML & Jscript: <HTML> <HEAD> <TITLE> Reading XML element values </TITLE> <SCRIPT LANGUAGE="JavaScript"> function readXMLDocument() { var xmldoc, meetingsNode, meetingNode, peopleNode var first_nameNode, last_nameNode, outputText xmldoc = new ActiveXObject("Microsoft.XMLDOM") xmldoc.load("meetings.xml") meetingsNode = xmldoc.documentElement meetingNode = meetingsNode.firstChild peopleNode = meetingNode.lastChild personNode = peopleNode.lastChild first_nameNode = personNode.firstChild last_nameNode = first_nameNode.nextSibling outputText = "Third name: " + first_nameNode.firstChild.nodeValue + ' ' + last_nameNode.firstChild.nodeValue messageDIV.innerHTML=outputText } </SCRIPT> </HEAD> <BODY> <CENTER> <H1> Reading XML element values </H1> <INPUT TYPE="BUTTON" VALUE="Get the name of the third person" ONCLICK="readXMLDocument()"> <P> <DIV ID="messageDIV"></DIV> </CENTER> </BODY> </HTML>
I accidentally discovered a solution. Before I "loaded" the document I set the ActiveXObject's "async" property to "false". That's a Microsoft DOMDocument object(what Microsoft calls the Document object) property not a W3C recommended one, by the way. I don't know why that made a difference, though. -med
|
 |
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
|
|
|
Probably because your script tried to access XML nodes before the whole XML document was loaded...
|
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
|
 |
Meadowlark Bradsher
Ranch Hand
Joined: Jan 23, 2001
Posts: 109
|
|
|
That was my assumption too.
|
 |
 |
|
|
subject: Rough Start
|
|
|