Solved

Help Needed!! XML and Cold Fusion

Posted on 2002-04-01
12
624 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
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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magento Indexing 1 89
Unattended XML settings 4 103
Create XML 5 47
c#  xml doc trying to pick out elements getting (Null reference exception when showing message box 6 38
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, …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

26 Experts available now in Live!

Get 1:1 Help Now