Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help Needed!! XML and Cold Fusion

Posted on 2002-04-01
12
Medium Priority
?
647 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
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!

 
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 210 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

885 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