Solved

Help Needed!! XML and Cold Fusion

Posted on 2002-04-01
12
631 Views
Last Modified: 2013-11-19
Hi, I am a newbie to XML. I need to do data retrieval from my ms access and display it on my html page. And this is the reason why i chose XML. Because i need to retrieve new data every few minutes, so I need to go back to the database. When I try run the javascript, there is a javascript error "Object Required".

I have attached the script below. Please take a look and tell me where have i done wrong.

<!--- jstest.htm --->
<html>

<head></head>
<body onload="init()">

<Script Language="JavaScript">
<!--//
function init()
{
   var announcement = new ActiveXObject("Microsoft.XMLDOM");
   announcement.async = false;
   announcement.load("createannouncement.cfm");
   var length = announcement.documentElement.childNodes.length;
   for(i=0; i<length; i++)
   {
      announceElement = announcement.documentElement.childNodes.item(i);    
      alert(announceElement.childNodes.item(0).childNodes(0).nodeValue);    
   }
     
}

//->
</Script>
</body>

</html>

<!--- CreateAnnouncement.cfm --->
<cfquery name="announce" datasource="InnoClient">
Select AnnouncementDialogue, URL From AnnouncementTable Where ID = 2
</cfquery>

<!--- create a temp variable, tempxml, to hold our xml doc.--->
<cfset tempxml = "<?xml version=""1.0""?>">
<cfset tempxml = tempxml & "<result>">
<cfoutput query="announce">
<cfset tempxml = tempxml & "
<announcement>
     <dialogue>#Trim("#XMLFormat(announce.AnnouncementDialogue)#")#</dialogue>
     <url>#Trim("#XMLFormat(announce.URL)#")#</url>
</announcement>
">
</cfoutput>
<cfset tempxml = tempxml & "</result>">

<!--- show the XML in the HTML output
<p>This is a simple XML document that's been generated by the CF code.</p>--->
<cfoutput>
<xmp>#tempxml#</xmp>
</cfoutput>

<!--- write out the XML --->
<cffile action="WRITE" file="c:\my documents\projects\xmlcftutorials\announcement.xml" output="#tempxml#">

Thanks.

Celine
0
Comment
Question by:celine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6911450
What is the XML Document like?

From what I see, there, this is the structure of your XML
<xmp>
<?xml version="1.0"?>
<result>
...
</result>
</xmp>

which is NOT valid XML for the MSXML Parser, the data should be
<?xml version="1.0"?>
<result>

</result>

Also, you should have some error handling

<script language="JavaScript">
<!--//
function init(){
var announcement = new ActiveXObject("Microsoft.XMLDOM");
announcement.async = false;
if (! announcement.load("createannouncement.cfm")) {
 var szMsg = "Error In Loading XML Tree";
 szMsg += "Error: " + announcement.parseError.errorCode;
 szMsg += "Reason: " + announcement.parseError.reason;
} else {
 var root = anouncement.documentElement;
 var length = root.childNodes.length;
 for(i=0; i<length; i++){
  announceElement = root.childNodes.item(i);    
  alert(announceElement.xml);      
 }
}

//->
</Script>



0
 

Author Comment

by:celine
ID: 6911842
I changed my codes to what you have suggest. But I still encounter an error.
Error: -1072896683
Reason: Only one top level element is allowed in an XML document.

Now, from the XML doc generated, which of the tag is the top level element? Is it <result>?

<!--- jstest.htm --->
<Script Language="JavaScript">
<!--//
function init()
{
  var announcement = new ActiveXObject("Microsoft.XMLDOM");
  announcement.async = false;
  if (! announcement.load("createannouncement.cfm"))
  {
    var szMsg = "Error In Loading XML Tree\n";
    szMsg += "Error: " + announcement.parseError.errorCode +"\n";
    szMsg += "Reason: " + announcement.parseError.reason;
    alert(szMsg);
  }
  else
  {
    var root = anouncement.documentElement;
    var length = root.childNodes.length;
    for(i=0; i<length; i++)
    {
       announceElement = root.childNodes.item(i);    
       alert(announceElement.xml);      
    }
  }
}


<!--- createannouncement.cfm --->
<cfquery name="announce" datasource="InnoClient">
Select AnnouncementDialogue, URL From AnnouncementTable Where ID = 2
</cfquery>

<?xml version="1.0"?>
<result>
<cfoutput query="announce">
<announcement>
     <dialogue>#Trim("#XMLFormat(announce.AnnouncementDialogue)#")#</dialogue>
     <url>#Trim("#XMLFormat(announce.URL)#")#</url>
</announcement>
</cfoutput>
</result>

Thus, the format of the xml will be

 <?xml version="1.0" ?>
- <result>
- <announcement>
  <dialogue>Hello1.</dialogue>
  <url />
  </announcement>
- <announcement>
  <dialogue>Welcome</dialogue>
  <url>http://www.domain.com</url>
  </announcement>
  </result>
0
 

Author Comment

by:celine
ID: 6911855
oh ya i forgot to close the <url>.. but still it is still the error reported.

generated XML doc....

<?xml version="1.0" ?>
- <result>
- <announcement>
 <dialogue>Hello1.</dialogue>
 <url />
 </announcement>
- <announcement>
 <dialogue>Welcome</dialogue>
 <url>http://www.domain.com></url>
 </announcement>
 </result>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:b1xml2
ID: 6912141
clear the browser cache and ensure that the <?xml version="1.0"?> is the first line of the output

0
 

Author Comment

by:celine
ID: 6912218
i tried clearing the browser cache and <?xml version="1.0"?> is the first line of the output but i still got the error.

The error src it prompted is <result>.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6912226
can you view the XML Output directly via MSIE5?
don't forget to set the mime type.
Under ASP
=========
Response.ContentType = "text/xml"
0
 

Author Comment

by:celine
ID: 6912247
Yes, I can the output correctly on my MSIE6.

But I am using cold fusion to generate the XML. Do I have to do it in ASP?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6912251
no, you dont.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6912256
try the following:-
<script language="javascript">
var announcement= new ActiveXObject("Microsoft.XMLDOM");
announcement.async = true;
announcement.onreadystatechange = showData;
announcement.load("createannouncement.cfm");

function showData() {
 if (announcement.readyState == 4) {
  if (announcement.parseError.errorCode != 0) {
   alert(announcement.parseError.errorCode);
  } else {
   alert(announcement.xml);
  }
 }

}
</script>

I suspect the following is causing your problems.

You need to send the whole XML document in one single chunk. Under ASP, the buffer is set to true and when the data is sent, it is sent in a single chunk. Your ColdFusion settings may have it being sent in bits and pieces. Amend your javascript code according to the sample and see the results.
0
 

Author Comment

by:celine
ID: 6912349
It still generate the same error code.

(Urghh, what could be the problem?)

0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 70 total points
ID: 6914971
change the progid to
<script language="javascript">
var announcement= new ActiveXObject("Msxml2.DOMDocument");
announcement.async = true;
announcement.onreadystatechange = showData;
announcement.load("createannouncement.cfm");

function showData() {
if (announcement.readyState == 4) {
 if (announcement.parseError.errorCode != 0) {
  alert(announcement.parseError.errorCode);
 } else {
  alert(announcement.xml);
 }
}

}
</script>

use it on the box that has MSIE6
0
 

Author Comment

by:celine
ID: 6947348
Well, I finally got the thing working. It isn't the javascript fault. Thanks anyway.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Questions about this React unit test 4 76
.php tree directory? 5 95
write xml in vb.net 2 49
xslt 1.0 - How to split value 8 48
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question