July 12, 2004

Reading an XML doc & Mach II

Reading an XML doc & Mach II

Let’s say it has come to a time, when you would like to read in an XML doc as a data source within the MachII framework. I'm going to use the XML doc from this previous post.

How does one go about doing that?

First within the model folder, set up the following structure

\input\xmlListener.cfc
\input\xmlGateway.cfc

Your xmlListener will look like:

<cfcomponent extends="MachII.framework.Listener" displayname="XML Listener" hint="I am the listener for xml Documents">

<cffunction name="configure" access="public" returntype="void" output="true" displayname="Blog Constructor" hint="I initialize this listener as part of the framework startup.">
<cfscript>
variables.xmlGateway = createObject("component","xmlReader.model.input.xmlGateway").init();
</cfscript>
</cffunction>

<cffunction name="getAll" access="public" returntype="query" output="false" displayname="Get XML doc" hint="I return a query containing the XML Doc.">
<cfreturn variables.xmlGateway.findAll() />
</cffunction>

<cffunction name="getTotalChildren" access="public" returntype="numeric" output="false" displayname="Get the Number of Aggregated Blogs" hint="I return the number of xmlChildren of the root">
<cfreturn variables.xmlGateway.findTotal() />
</cffunction>
</cfcomponent>

Your xmlGateway will appear as:

<cfcomponent displayname="XML Gateway" hint="I am a data gateway to Summarize an XML document">

<cffunction name="init" access="public" returntype="xmlReader.model.input.xmlGateway" output="false" displayname="Gateway Constructor" hint="I initialize the XML gateway.">
<cfset var inputXML = "" />
<cfset variables.structXML = 0 />
<cffile action="read" variable="inputXML" file="#ExpandPath("data\xmlDOC.xml")#" />
<cfset variables.structXML = XMLParse(inputXML) />
<cfreturn this />
</cffunction>

<cffunction name="findAll" access="public" returntype="query" output="false" displayname="Find All" hint="I return a query containing the XML Doc">
<cfset var qrySelect = 0 />
<cfset var structListing = 0 />
<cfset var patientName = 0 />
<cfset var patientID = 0 />
<cfset var visitID = 0 />
<cfset var admitDate = 0 />
<cfset var payor = 0 />
<cfset var nurseStation = 0 />
<cfset var roomBed = 0 />
<cfset var service = 0 />
<cfset var previousDischargeDate = 0 />
<cfset var selectRow = 0 />

<cfset structListing = XMLSearch(structXML, "/ReadmissionReport/Readmission") />
<cfset qrySelect = QueryNew("patientName, patientID, visitID, admitDate, payor, nurseStation, roomBed, service, perviousDischargeDate") />

<cfloop from="1" to="#ArrayLen(structListing)#" index="i">
<cfset patientName = structListing[i].PatientName.XmlText />
<cfset patientID = structListing[i].PatientID.XmlText />
<cfset visitID = structListing[i].VisitID.XmlText />
<cfset admitDate = structListing[i].AdmitDate.XmlText />
<cfset payor = structListing[i].Payor.XmlText />
<cfset nurseStation = structListing[i].NurseStation.XmlText />
<cfset roomBed = structListing[i].RoomBed.XmlText />
<cfset service = structListing[i].Service.XmlText />
<cfset selectRow = QueryAddRow(qrySelect) />

<cfset QuerySetCell(qrySelect, "patientName", patientName, selectRow) />
<cfset QuerySetCell(qrySelect, "patientID", patientID, selectRow) />
<cfset QuerySetCell(qrySelect, "visitID", visitID, selectRow) />
<cfset QuerySetCell(qrySelect, "admitDate", admitDate, selectRow) />
<cfset QuerySetCell(qrySelect, "payor", payor, selectRow) />
<cfset QuerySetCell(qrySelect, "nurseStation", nurseStation, selectRow) />
<cfset QuerySetCell(qrySelect, "roomBed", roomBed, selectRow) />
<cfset QuerySetCell(qrySelect, "service", service, selectRow) />
</cfloop>

<cfreturn qrySelect />
</cffunction>

<cffunction name="findTotal" access="public" returntype="numeric" output="false" displayname="Find Total" hint="I return the number of readmits in the xml file">
<cfset var xmlNode = 0 />

<cfset xmlNode = structXML.XMLRoot>
<cfreturn ArrayLen(xmlNode.xmlChildren)>
</cffunction>

</cfcomponent>

Next within the config folder, add the following to the machii.xml under Listeners.

<listener name="xmlListener" type="xmlReader.model.input.xmlListener"> <invoker type="MachII.framework.invokers.CFCInvoker_Event" /> </listener>

And add this under event handler to call the listener to the desired event

<notify listener="xmlListener" method="getTotalChildren” resultKey="request.intTotal" /> <notify listener="xmlListener" method="getAll" resultKey="request.qryAll" />

And within the view add the following logic to see the total and query

#request.intTotalChildren# #request.qryAll#

Posted by Elyse at July 12, 2004 6:34 AM | TrackBack
Comments

I am not sure this would be how I would model it -- Data manipulations would be in the DAO and Gateway helper objects to a more real-world modelled listener, like let's say a "User" object.

Posted by: Hugo Ahlenius at July 15, 2004 12:15 AM
Post a comment









Remember personal info?