Solved

Help Needed!! XML and Cold Fusion

Posted on 2002-04-01
12
622 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
  • 6
  • 6
12 Comments
 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 23

Expert Comment

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

0
 

Author Comment

by:celine
Comment Utility
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
Comment Utility
can you view the XML Output directly via MSIE5?
don't forget to set the mime type.
Under ASP
=========
Response.ContentType = "text/xml"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:celine
Comment Utility
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
Comment Utility
no, you dont.
0
 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
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
Comment Utility
It still generate the same error code.

(Urghh, what could be the problem?)

0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 70 total points
Comment Utility
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
Comment Utility
Well, I finally got the thing working. It isn't the javascript fault. Thanks anyway.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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)

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now