This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Microsoft XML > September 2005 > XML document error





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author XML document error
Jacques Cooper

2005-09-24, 4:21 am

Hello,

I found this sample script file, it works on the publisher's web site,
but it doesn't work on my desktop.

I get the following error, when I try to load the test.htm script in IE 6.0:
"Error: 'document.getElementById(...)' is null or not an object"
(see function createTable())

Any ideas on how to fix this error?

TIA,
Jacques


/////////////////////
// emperors.xml
////////////////////
<?xml version="1.0"?>
<emperors>
<emperor>
<name>Augustus</name>
<rule>27BC-14AD</rule>
<death>Peaceful</death>
</emperor>
<emperor>
<name>Tiberius</name>
<rule>14-37</rule>
<death>Murdered by his great-nephew and the commander of his
bodyguard</death>
</emperor>
<emperor>
<name>Caligula</name>
<rule>37-41</rule>
<death>Murdered by the commander of his bodyguard (a different one than
the one before)</death>
</emperor>
<emperor>
<name>Claudius</name>
<rule>41-54</rule>
<death>Poisoned by his fourth wife</death>
</emperor>
<emperor>
<name>Nero</name>
<rule>54-68</rule>
<death>Suicide after losing the Empire</death>
</emperor>
</emperors>


///////////////////////////////
// test.htm
///////////////////////////////
<html>
<head>

<script language="javascript">

function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can't handle this script');
return;
}

xmlDoc.load("emperors.xml");
}

function createTable()
{
var x = xmlDoc.getElementsByTagName('emperor');
var newEl = document.createElement('TABLE');
newEl.setAttribute('cellPadding',5);
var tmp = document.createElement('TBODY');
newEl.appendChild(tmp);
var row = document.createElement('TR');
for (j=0;j<x[0].childNodes.length;j++)
{
if (x[0].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TH');
var theData = document.createTextNode(x[0].childNodes[j].nodeName);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
for (i=0;i<x.length;i++)
{
var row = document.createElement('TR');
for (j=0;j<x[i].childNodes.length;j++)
{
if (x[i].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TD');
var theData =
document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
}
/////// Error: 'document.getElementById(...)' is null or not an object
document.getElementById('writeroot').appendChild(newEl);
}
</script>

<title>Test</title>
</head>

<body onLoad="importXML()">

</body>
</html>


Joe Fawcett

2005-09-24, 7:17 am

"Jacques Cooper" <jcooper@jcsoftware.net> wrote in message
news:%23NOJAXNwFHA.460@TK2MSFTNGP15.phx.gbl...
> Hello,
>
> I found this sample script file, it works on the publisher's web site,
> but it doesn't work on my desktop.
>
> I get the following error, when I try to load the test.htm script in IE 6.0:
> "Error: 'document.getElementById(...)' is null or not an object"
> (see function createTable())
>
> Any ideas on how to fix this error?
>
> TIA,
> Jacques
>


Doesn't there need to be a div with an id of "writeroot" in the HTML body?

--

Joe (MVP)

https://mvp.support.microsoft.com/p...E8-8741D22D17A5
>
> /////////////////////
> // emperors.xml
> ////////////////////
> <?xml version="1.0"?>
> <emperors>
> <emperor>
> <name>Augustus</name>
> <rule>27BC-14AD</rule>
> <death>Peaceful</death>
> </emperor>
> <emperor>
> <name>Tiberius</name>
> <rule>14-37</rule>
> <death>Murdered by his great-nephew and the commander of his
> bodyguard</death>
> </emperor>
> <emperor>
> <name>Caligula</name>
> <rule>37-41</rule>
> <death>Murdered by the commander of his bodyguard (a different one than the
> one before)</death>
> </emperor>
> <emperor>
> <name>Claudius</name>
> <rule>41-54</rule>
> <death>Poisoned by his fourth wife</death>
> </emperor>
> <emperor>
> <name>Nero</name>
> <rule>54-68</rule>
> <death>Suicide after losing the Empire</death>
> </emperor>
> </emperors>
>
>
> ///////////////////////////////
> // test.htm
> ///////////////////////////////
> <html>
> <head>
>
> <script language="javascript">
>
> function importXML()
> {
> if (document.implementation && document.implementation.createDocument)
> {
> xmlDoc = document.implementation.createDocument("", "", null);
> xmlDoc.onload = createTable;
> }
> else if (window.ActiveXObject)
> {
> xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
> xmlDoc.onreadystatechange = function () {
> if (xmlDoc.readyState == 4) createTable()
> };
> }
> else
> {
> alert('Your browser can't handle this script');
> return;
> }
>
> xmlDoc.load("emperors.xml");
> }
>
> function createTable()
> {
> var x = xmlDoc.getElementsByTagName('emperor');
> var newEl = document.createElement('TABLE');
> newEl.setAttribute('cellPadding',5);
> var tmp = document.createElement('TBODY');
> newEl.appendChild(tmp);
> var row = document.createElement('TR');
> for (j=0;j<x[0].childNodes.length;j++)
> {
> if (x[0].childNodes[j].nodeType != 1) continue;
> var container = document.createElement('TH');
> var theData = document.createTextNode(x[0].childNodes[j].nodeName);
> container.appendChild(theData);
> row.appendChild(container);
> }
> tmp.appendChild(row);
> for (i=0;i<x.length;i++)
> {
> var row = document.createElement('TR');
> for (j=0;j<x[i].childNodes.length;j++)
> {
> if (x[i].childNodes[j].nodeType != 1) continue;
> var container = document.createElement('TD');
> var theData =
> document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
> container.appendChild(theData);
> row.appendChild(container);
> }
> tmp.appendChild(row);
> }
> /////// Error: 'document.getElementById(...)' is null or not an object
> document.getElementById('writeroot').appendChild(newEl);
> }
> </script>
>
> <title>Test</title>
> </head>
>
> <body onLoad="importXML()">
>
> </body>
> </html>
>



Martin Honnen

2005-09-24, 6:20 pm



Jacques Cooper wrote:

> I get the following error, when I try to load the test.htm script in IE 6.0:
> "Error: 'document.getElementById(...)' is null or not an object"


> /////// Error: 'document.getElementById(...)' is null or not an object
> document.getElementById('writeroot').appendChild(newEl);


> <body onLoad="importXML()">
>
> </body>



Not an XML problem or question at all, your client-side script in the
HTML document tries to access an element in the HTML document which has
the id attribute value 'writeroot' (with
document.getElementById('writeroot') but there is no such element, thus
the call returns null and any attempt to call appendChild on that gives
the error.

With that HTML example you probably simply want
document.body.appendChild(newEl)
instead of
document.getElementById('writeroot').appendChild(newEl)
or you would need to add some element with id 'writeroot' first.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jacques Cooper

2005-09-24, 6:20 pm

Dear Mr. Honnen,

Thank you for your quick response.

> With that HTML example you probably simply want
> document.body.appendChild(newEl)
> instead of
> document.getElementById('writeroot').appendChild(newEl)
> or you would need to add some element with id 'writeroot' first.


Yes, that simple replacement fixed the problem.

Thanks again,
Jacques


Jacques Cooper

2005-09-24, 6:20 pm

Dear Mr. Honnen,

I have another problem. I noticed that when I delete data
from a XML attribute, I receive the following error message:

"Error: Object required"
var theData =
document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);

For example, when I remove "Augustus" from the <name> attribute this causes
the above
mentioned error.

<emperor>
<name></name>
<rule>27BC-14AD</rule>
<death>Peaceful</death>
</emperor>

What data value can I use to replace the "name" (I tried a blank space, and
that did not solve the problem)?
Or, how can I modify the XML script to ignore blank values?

Thanks,
Jacques


Sam Hobbs

2005-09-25, 3:21 am

For anyone that might want to answer this question, it was reposted three
hours later as a new thread with the subject "XML DOM Error". Therefore
there might be answers there.


"Jacques Cooper" <jcooper@jcsoftware.net> wrote in message
news:eRat8iTwFHA.3864@TK2MSFTNGP12.phx.gbl...
> Dear Mr. Honnen,
>
> I have another problem. I noticed that when I delete data
> from a XML attribute, I receive the following error message:
>
> "Error: Object required"
> var theData =
> document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
>
> For example, when I remove "Augustus" from the <name> attribute this
> causes the above
> mentioned error.
>
> <emperor>
> <name></name>
> <rule>27BC-14AD</rule>
> <death>Peaceful</death>
> </emperor>
>
> What data value can I use to replace the "name" (I tried a blank space,
> and that did not solve the problem)?
> Or, how can I modify the XML script to ignore blank values?
>
> Thanks,
> Jacques
>



Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews