Title: Retrieving Root attribute
Ken,
 
Copy this into a CF template, save it, and run it. It'll return what you need. Also, if CLASS="Msxml2.DOMDocument.4.0"  doesn't work, try replacing it with CLASS="Microsoft.XMLDOM".
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
 <title>Get the root's attributes</title>
</head>
 
<body>
<cfsavecontent variable="xmldata">
<ROOT DATE_CREATED="2003-02-19 19:05:23" CREATED_BY="Microsoft Log Parser V2.0">
<ROW>
  <Field1>Field 1</Field1>
  <Field2>Field 2</Field2>
  <Field3>Field 3</Field3>
  </ROW>
  </ROOT>
</cfsavecontent>
 
<CFTRY>
    <!--- If it exists, connect to it --->
    <CFOBJECT  ACTION=""   CLASS="Msxml2.DOMDocument.4.0" NAME="objXML"  TYPE="COM">
  <CFCATCH>
    <!--- The object doesn't exist, so create it --->
    <CFOBJECT
        ACTION=""  CLASS="Msxml2.DOMDocument.4.0" NAME="objXML"  TYPE="COM">
  </CFCATCH>
</CFTRY>
 
<cfscript>
  createdOn="";
  createdBy="";
 objXML.async = false;
 objXML.LOADXML(#xmldata#); 
 root = objXML.documentElement;
 
createdOn = root.getAttribute("DATE_CREATED");
createdBy = root.getAttribute("CREATED_BY");
writeOutput("createdOn = " & createdOn);
writeOutput("<br>");
writeOutput("createdBy = " & createdBy);
</cfscript>
 

</body>
</html>
 
Good luck.
 
Marc
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Suyer, Ed [PRD Non-J&J]
Sent: Thursday, February 20, 2003 11:05 AM
To: '[EMAIL PROTECTED]'
Subject: RE: [cf-xml] Retrieving Root attribute

Hey Ken,
 
Here's the CF code you'll need.  Not sure if all of this is available in CF5 ... I use this with CFMX:
 
  <!--- read and parse xml file into cf structure --->
  <cffile action="" file="myxmlfile.xml" variable="xmldoc">
  <cfset mydoc = XmlParse(xmldoc)>
   
   <!--- at this point mydoc is a cf structure that contains properties for each element in the xml doc.  the following command let's you view this struc in mx.  In cf5, you'll probobally need to use the structure functions to debug ref: http://localhost/cfdocs/CFML_Reference/functions-pt020.html#1099964  --->
<cfdump var="#mydoc#">
 
<!--- the following is one way to then extract xml elements into cf variables --->
    local.createddate = mydoc.XmlRoot.type.XmlText;
 
 
<!--- also, here's a way to loop through your row elements and fields --->
 
<cfloop index="i" from="1" to="#ArrayLen(mydoc.XmlRoot.XmlChildren)#">
   <!--- parse ea element for argument names and bind --->
    <cfset local.fieldvalue = mydoc.XmlRoot.XmlChildren[i].XmlText>
 </cfloop>
 
Hope this helps.
 
Best regards,
 
Ed.
-----Original Message-----
From: Thomason, Ken L. [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 19, 2003 2:50 PM
To: '[EMAIL PROTECTED]'
Subject: [cf-xml] Retrieving Root attribute


I am relatively new to XML and the XML Toolkit. I am currently able to retrieve elements from an XML structure I am working with but I am not sure how to retrieve an attribute of the ROOT node.

For example this is a snippet of the XML file I am working with:
<?xml version="1.0" standalone="yes" ?>
  <!DOCTYPE ROOT (View Source for full doctype...)>
- <ROOT DATE_CREATED="2003-02-19 19:05:23" CREATED_BY="Microsoft Log Parser V2.0">
- <ROW>
  <Field1>Field 1</Field1>
  <Field2>Field 2</Field2>
  <Field3>Field 3</Field3>
  </ROW>
  </ROOT>

I would like to be able to retrieve the DATE_CREATED attribute for the XML document.
As you can see the XML doc is created by Microsoft's Log Parser and I am using the XML toolkit with Cold Fusion 5.

Thanks in advance for you help,
Ken

Reply via email to