Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1040387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:13:03+00:00 2026-05-16T15:13:03+00:00

UPDATED CODE TO LATEST ITERATION The following function consumes a webservice that returns address

  • 0

UPDATED CODE TO LATEST ITERATION

The following function consumes a webservice that returns address details based on zip code (CEP). I’m using this function to parse the xml and populate an empty query with the address details. I would like to know if there is a more elegant way to achieve the same result. It seems to be a waste to create an empty query and populate it…

Any ideas could my method be modified or the code factored/simplified?

<!--- ****** ACTION: getAddress (consumes web-service to retrieve address details) --->
<cffunction name="getAddress" access="remote" returntype="any" output="false">

    <!--- Defaults: strcep (cep (Brazilian zip-code) string webservice would look for), search result returned from webservice --->
    <cfargument name="cep" type="string" default="00000000">
    <cfset var searchResult = "">
    <cfset var nodes = "">
    <cfset var cfhttp = "">
    <cfset var stateid = 0>
    <cfset var tmp = structNew()>

    <!--- Validate cep string --->
    <cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>

        <cftry>

            <!--- Consume webservice --->                
            <cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#"></cfhttp>
            <cfset searchResult = xmlparse(cfhttp.FileContent)>
            <cfset nodes = xmlSearch(searchResult, "//tbCEP")>

            <!--- If result insert address data into session struct --->
            <cfif arrayLen(nodes)>

                <cfset tmp.streetType = nodes[1].logradouro.XmlText>
                <cfset tmp.streetName = nodes[1].nome.XmlText>
                <cfset tmp.area = nodes[1].bairro.XmlText>
                <cfset tmp.city = nodes[1].cidade.XmlText>
                <cfset tmp.state = nodes[1].uf.XmlText>
                <cfset tmp.cep = arguments.cep>

                <!--- Get state id and add to struct --->
                <cfset stateid = model("state").findOneByStateInitials(tmp.state)>
                <cfset tmp.stateid = stateid.id>

                <cfreturn tmp>

            </cfif>

            <!--- Display error if any --->
            <cfcatch type="any">
                <cfoutput>
                    <h3>Sorry, but there was an error.</h3>
                    <p>#cfcatch.message#</p>
                </cfoutput>
            </cfcatch>

        </cftry>

    </cfif>

</cffunction>
<!--- ****** END ACTION getAddress --->

The calling code:

        <!--- Get address data based on CEP --->
        <cfset session.addressData = getAddress(cep=params.newMember.cep)>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:13:04+00:00Added an answer on May 16, 2026 at 3:13 pm

    I can’t test this because I don’t have an example XML file / CEP to test with, but here is a minor rewrite that addresses four things:

    • Instead of using cfparam and some strange “params” structure, you should pass the CEP into the function as an argument.
    • The function shouldn’t directly modify session data. Instead, you should return the result and let the calling code assign it to the session (or wherever else it might be needed). I’ll show this in a 2nd code example.
    • Cache the xml result per CEP — assuming this doesn’t change often. (You’ll have to improve it further if you want time-based manual cache invalidation, but I can help add that if necessary)
    • Don’t use StructInsert. It’s not necessary and you’re just writing it the long way for the sake of writing it the long way. There is no benefit.

    Again, this isn’t tested, but hopefully it’s helpful:

    <cffunction name="getAddress" access="remote" returntype="any" output="false">
        <cfargument name="cep" type="string" default="00000000" /><!--- (cep (Brazilian zip-code) string webservice would look for) --->
        <cfset var searchResult = "">
        <cfset var nodes = "">
        <cfset var cfhttp = "">
        <cfset var stateid = 0 />
        <cfset var tmp = structNew()>
    
        <!--- Validate cep string --->
        <cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>
    
            <cfif not structKeyExists(application.cepCache, arguments.cep)><!--- or cache is expired: you'd have to figure this part out --->
    
                <!--- Consume webservice --->
                <cftry>
                    <cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#" />
                    <cfset searchResult = xmlparse(cfhttp.FileContent)>
                    <cfset nodes = xmlSearch(searchResult, "//tbCEP")>
    
                    <!--- If result insert address data into session struct --->
                    <cfif arrayLen(nodes)>
    
                        <cfset tmp.streetType = nodes[1].logradouro.XmlText />
                        <cfset tmp.streetName = nodes[1].nome.XmlText />
                        <cfset tmp.area = nodes[1].bairro.XmlText />
                        <cfset tmp.city = nodes[1].cidade.XmlText />
                        <cfset tmp.state = nodes[1].uf.XmlText />
                        <cfset tmp.cep = arguments.cep />
    
                        <!--- Get state id and add to struct --->
                        <cfset stateid = model("state").findOneByStateInitials(session.addressData.state)>
                        <cfset tmp.stateid = stateid.id />
    
                    </cfif>
    
                    <cfreturn duplicate(tmp) />
    
                    <!--- Display error if any --->
                    <cfcatch type="any">
                        <h3>Sorry, but there was an error.</h3>
                        <p>#cfcatch.message#</p>
                    </cfcatch>
    
                </cftry>
    
            <cfelse>
    
                <!--- cache exists and is not expired, so use it --->
                <cfreturn duplicate(application.cepCache[arguments.cep]) />
    
            </cfif>
    
        </cfif>
    <!---
        <!--- Redirect to page two of the sign up process --->
        <cfset redirectTo(controller="assine", action="perfil")>
    --->
    
    </cffunction>
    

    Notice that I commented out the redirect you had at the end. That’s because with my function, you’ll be returning a value, and the redirect should be done after that, like so:

    <cfset session.addressData = getAddress("some-CEP-value") />
    <cfset redirectTo(controller="assine", action="perfil")>
    

    If you’re going to leave out the caching (As you say in a comment you will), then here is a version that makes no attempt at caching:

    <cffunction name="getAddress" access="remote" returntype="any" output="false">
        <cfargument name="cep" type="string" default="00000000" /><!--- (cep (Brazilian zip-code) string webservice would look for) --->
        <cfset var searchResult = "">
        <cfset var nodes = "">
        <cfset var cfhttp = "">
        <cfset var stateid = 0 />
        <cfset var tmp = structNew()>
    
        <!--- Validate cep string --->
        <cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>
    
            <!--- Consume webservice --->
            <cftry>
                <cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#" />
                <cfset searchResult = xmlparse(cfhttp.FileContent)>
                <cfset nodes = xmlSearch(searchResult, "//tbCEP")>
    
                <!--- If result insert address data into session struct --->
                <cfif arrayLen(nodes)>
    
                    <cfset tmp.streetType = nodes[1].logradouro.XmlText />
                    <cfset tmp.streetName = nodes[1].nome.XmlText />
                    <cfset tmp.area = nodes[1].bairro.XmlText />
                    <cfset tmp.city = nodes[1].cidade.XmlText />
                    <cfset tmp.state = nodes[1].uf.XmlText />
                    <cfset tmp.cep = arguments.cep />
    
                    <!--- Get state id and add to struct --->
                    <cfset stateid = model("state").findOneByStateInitials(session.addressData.state)>
                    <cfset tmp.stateid = stateid.id />
    
                </cfif>
    
                <cfreturn duplicate(tmp) />
    
                <!--- Display error if any --->
                <cfcatch type="any">
                    <h3>Sorry, but there was an error.</h3>
                    <p>#cfcatch.message#</p>
                </cfcatch>
    
            </cftry>
    
        </cfif>
    
    <!---
        <!--- Redirect to page two of the sign up process --->
        <cfset redirectTo(controller="assine", action="perfil")>
    --->
    
    </cffunction>
    

    Note that I did leave in the use of duplicate(). What this does is return a duplicate of the object (in this case, the struct). This is much more important when you start to work on applications where you’re passing complex values into and out of functions over and over again. Using duplicate() causes things to be passed by value instead of by reference. It may not bite you in this case, but it’s a good habit to get into.

    I would also still use the function argument and return a value — but it’s arguable that this is my personal preference. In a way it is. I believe that a function should be fully encapsulated; a total “black box”. You give it some input and it gives you back some output. It should not modify anything outside of itself. (Again, just my opinion.)

    So assuming you’re using this function as part of a larger multi-step process, you should still use it the same way I’ve described above. The only difference is that you’re setting the session variable outside of the function body. Just as previously:

    <cfset session.addressData = getAddress("some-CEP-value") />
    <cfset redirectTo(controller="assine", action="perfil")>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

seems having updated to the latest version of the facebook php sdk the following
I am looking for a perforce integrate/get-latest command which would fetch the latest/updated-code and
UPDATED My latest code as follows: $file =sqlite:C:\New folder\\test.db; echo $file; $handle = new
The code i current have is this. function update (){ latest_id = $('#image:first').data('position'); /*
I have updated code for ActiveX functionality which already installed on client(in their Windows
EDIT : Scroll down to see the updated code. I would like to build
I updated my code with string dates created with new Date and added back
I updated my code like Andro wrote about to me: public class ZiiziiActivity extends
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.