Title: Parsing XML As Query Object In Coldfusion Word Count: 1009 Summary: I work in the financial industry as a developer and not that the financial industry has any greater implementation of XML than any other, but web applications that utilize back office processing for loan applications, or new account opening processes, or retail internet banking applications all to some extent make calls to a host
Title: Parsing XML As Query Object In Coldfusion Word Count: 1009 Summary: I work in the financial industry as a developer and not that the financial industry has any greater implementation of XML than any other, but web applications that utilize back office processing for loan applications, or new account opening processes, or retail internet banking applications all to some extent make calls to a host.
Many times it's a DB2 database, and many times some other process that returns account information or validation information in xml format. One ... Keywords: ColdFusion,Ben Cortese, XML, SQL Article Body: I work in the financial industry as a developer and not that the financial industry has any greater implementation of XML than any other, but web applications that utilize back office processing for loan applications, or new account opening processes, or retail internet banking applications all to some extent make calls to a host.
Many times it's a DB2 database, and many times some other process that returns account information or validation information in xml format. One of the fun things that I found working in ColdFusion is the many ways you can manipulate xml data. I'm certain this type of process isn't exclusive to ColdFusion, but I wanted to put it out there anyway.
Many developers have a little apprehension about working with new technologies at first, or trying something that hasn't been already tried and true and of course documented heavily
Many developers have a little apprehension about working with new technologies at first, or trying something that hasn't been already tried and true and of course documented heavily. And frankly it's hard to find the extra time with a heavy work load and deadlines to meet to go off experimenting But I managed to put aside some time to look at parsing XML into an object that developers are all familiar with, the Query object.
As a developer, writing SQL is common place, so I figured maybe looking at a hybrid approach to XML and SQL in ColdFusion, might prove to offer some benefit. So I'll show an example of reading in a Phonebook xml document. It will look like this: You can copy this and save it off as phonebook.xml if you like.
<?xml version="1.0"?> <phonebook> <contact category="friend"> <firstName>John</firstName> <lastName>Smith</lastName> <phone>412-555-1212</phone> <email> Johnsmith@email.comjanesmith@email.combob-jones@mailserver.comBill.johnson@someserver.comhttp://turkiyespot.com/jackrobinson.com </a> </e -posta> </iletişim> </Phonebook> XML'de okumak sadece bu küçük kod bloğunu kullanarak kısa ve tatlıdır.
<cfhttp url = " http://turkiyespot
<cfhttp url = " http://turkiyespot.com/ http://turkiyespot.com/mywebsite.com/myxmldocs/phonebook.xml</a></a>" method="GET" resolveurl="No" ></cfhttp> <cfset mydoc = XmlParse(CFHTTP.FileContent)> Now you've got the xml object in a defined variable name called "mydoc". Next you set a variable to contain the child element nodes of the xml document and do the same to define the size of the document, which you will see in a bit as to what it's used for.
<cfset pb = http://turkiyespot.com/mydoc.phonebook.xmlchildren </a>> <cfset boyutu = arraylen (pb)> Şimdi telefon defteri verileriyle bir sorgu nesnesi oluşturun.Gördüğünüz gibi, XML "sütun adları" içeren MyQuery nesnesini oluşturmak için düğüm özelliklerinin ve adlarının ne olduğunu zaten biliyoruz.Verileri veritabanı gibi bir yapı ile ilişkilendirmek için sütun adları diyorum.
<cfet myQuery = QueryNew ("Cat, First Name, LastName, Telefon, E -posta")>> Şimdi sorgu nesnesinin "sütunlarına" sahip olduğumuza göre, BOYUT nesnesini bize bu tür satırları tanımlamak için parametreleri vermek için kullanıyoruz. <cfset temp = QueryAddrow (myQuery, #size #)> Şimdi bir sorgu nesnesi 'sıcaklık' oluşturdunuz.
Şimdi sorgu nesnesini XML belgesindeki verilerle doldurduğunuz kısım geliyor
Şimdi sorgu nesnesini XML belgesindeki verilerle doldurduğunuz kısım geliyor.Nesnedeki her "satır" ın değerini XML nesnesinden bir dizi gibi ekleyerek döndürün. <cfloop index = "i" from = "1" ila = #beden #> <cfset temp = querysetcell (myquery, "kedi", #mydoc.phonebook.contact [i] .xmlattributes ['kategori']#, #i#)> <cfset temp = querysetcell (myquery, "firstname", #mydoc.phonebook.contact [i] .firstname.xmltext#, #i#)> <cfset temp = querysetcell (myQuery, "LastName", #mydoc.phonebook.contact [i] .lastname.xmltext#, #i#)> <cfset temp = querysetcell (myquery, "telefon", #mydoc.phonebook.contact [i] .phone.xmltext#, #i#)> <cfset temp = querysetcell (myquery, "e -posta", #mydoc.phonebook.contact [i].
</cfloop> Aslında az önce yaptığınız şey, XML'yi almak ve bir veritabanını sorguladığınızda sahip olacağınız aynı veri yapısına dönüştürmektir. Gerisi, büyük bir uygulama olan ColdFusion bileşenlerini kullanan birçok ColdFusion geliştiricisine aşina olmalıdır. CFInvoke kullanarak sorgu işlevini arayın.Bu özel işlev, telefon defteri bireylerini soyadına göre sıralayacağınızdır.
<cfinvoke bileşeni = "pbook_meths" yöntem = "sortlname" returnVariable = "sonuç"> <cfinvokeargument name = "q_obj" değer = "#myQuery#">> </cfinvoke> İşlevden gelen sonucun çıkışı normal bir SQL sorgusundan farklı olmayacaktır. <tablo sınırı = 1 genişlik = 500 hizalama = merkez> <th> kategori </th> <th> yumruk adı </th> <th> soyadı </th> <th> telefon </th> <th> E -posta </th> <cfutput query = "sonuç"> <tr> <td> #cat#</td> <td> #firstname#</td> <td> #lastname#</td> <td>#telefon#</td> <td> #Email#</td> </tr> </cfutput> </tablo> Bileşenler ColdFusion'da olduğu ve böyle görüneceği için çağrılan işlev kendi dosyasında saklanır.
<cffunction name = "sortlname" Access = "Remote" returnType = "Sorgu">> <cfargument name = "q_obj" gerekli = "evet"> <cftry> <cfQuery name = "pbtest" dbtype = "sorgu"> SEÇME * Argümanlardan
<cffunction name = "sortlname" Access = "Remote" returnType = "Sorgu">> <cfargument name = "q_obj" gerekli = "evet"> <cftry> <cfQuery name = "pbtest" dbtype = "sorgu"> SEÇME * Argümanlardan.q_obj LastName tarafından sipariş, kedi </cfquery> <cfcatch type = "any"> <p> <cfutput>#cfcatch.message#</cfutput> </p> </cfcatch> </cftry> <cfreturn pbtest> </cffunction> Bileşeniniz, XML sorgu nesnesini bir veritabanından normal bir sorgu ile aynı şekilde manipüle eden çok sayıda işlev içerebilir.İlişkisel bir veritabanının elbette sunduğu gücünüz yoktur, ancak SQL yazmaktan rahatsanız, kendinizi ColdFusion'da XML ile çalışarak koşarken yere vururken bulabilirsiniz.
Teşekkürler ve mutlu kodlama.
Profesyonel hizmetlerimiz hakkında daha fazla bilgi için hemen iletişime geçin!