Solved

Help Needed!! XML and Cold Fusion

Posted on 2002-04-01
12
632 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

688 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