Solved

Parse and output XML

Posted on 2004-10-29
182 Views
Last Modified: 2013-12-24
Hello all,

I'm trying to build an address look up app from xml files provided from postcodeanywhere.co.uk. I know I need to execute a lookup query with cfhttp and then parse it  but I need some help on how to output the address options... I keep on running into a multitude of errors. The format of the xml files can be viewed here: http://www.postcodeanywhere.co.uk/downloads/developer/samples/xml_examples.doc

Can anyone provide me with some pointers for the first two xml docs detailed?

Cheers, P
0
Question by:areola
    4 Comments
     
    LVL 7

    Expert Comment

    by:INSDivision6
    Not clear what is your problem/question.
    0
     
    LVL 17

    Accepted Solution

    by:
    This data should come from a cfhttp request, but for demonstration purposes I put the xml in a variable
    <cfset variables.myXMLString = "<PostcodeAnywhere>
             <Schema Items="3">
                        <Field Name="id" />
                        <Field Name="seq" />
                       <Field Name="description" />
             </Schema>
             <Data Items="45">
                  <Item id="47264561" seq="0" description="Arthur Amos Associates, Unit 5, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264564" seq="1" description="Axis Milk Ltd, 11-13 Elgar Business Centre, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264560" seq="2" description="Computer Counter Ltd, Monkwood House, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264566" seq="3" description="Fernhill Solutions Ltd, Enigma House, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264565" seq="4" description="Microtrol Ltd, Unit 16, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264547" seq="5" description="Moseley Saw Mills, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264563" seq="6" description="Premier Recoveries Ltd, Premier House, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264556" seq="7" description="Robin Pearce, Worlds End Nurseries, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264562" seq="8" description="Willowbrook, Mill House, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264546" seq="9" description="Worlds End Nurseries, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="48040720" seq="10" description="Wychburn Engineering Services Ltd, Unit 2, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264555" seq="11" description="(Cottrell), Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264559" seq="12" description="1 The Cottages, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264558" seq="13" description="2 The Cottages, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="29459748" seq="14" description="3 Elgar Business Centre, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264527" seq="15" description="Beaumont, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264528" seq="16" description="Brook Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264529" seq="17" description="Cherry Tree Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="48276297" seq="18" description="Unit 7, Elgar Business Centre, Moseley Road, Worcester WR2 6NJ" />
                  <Item id="47264530" seq="19" description="Essfield, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264531" seq="20" description="Fairfields, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264544" seq="21" description="Flatten Bank, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264545" seq="22" description="Four Acres, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264532" seq="23" description="Grosmont Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264526" seq="24" description="Holly Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264549" seq="25" description="Hollymount, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264533" seq="26" description="Inglemere, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264534" seq="27" description="Ivy Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264535" seq="28" description="Kothi, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264536" seq="29" description="Leacroft, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264537" seq="30" description="Lyncroft, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264538" seq="31" description="Maryville, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264539" seq="32" description="Moseley Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264552" seq="33" description="Orchard View, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264557" seq="34" description="Pump Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264525" seq="35" description="Rose Cottage, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264548" seq="36" description="Rose Dale, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264540" seq="37" description="Sandfield, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264541" seq="38" description="Stilecroft, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264553" seq="39" description="The Haven, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264554" seq="40" description="The Old Bakery, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264551" seq="41" description="The Orchard, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264550" seq="42" description="The Willows, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264542" seq="43" description="Whitegates, Moseley Road, Hallow, Worcester WR2 6NJ" />
                  <Item id="47264543" seq="44" description="Wylcotts, Moseley Road, Hallow, Worcester WR2 6NJ" />
            </Data>
    </PostcodeAnywhere>
    ">

    <cfset objXML = xmlParse( variables.myXMLString ) />
    You now have an in memory representation of the XML, and you can now access its values.

    #variables.myXMLString.xmlRoot.Data.Item[ 1 ].xmlAttributes.description#
    This is how you output a attribute

    <cfloop from="1" to="#arrayLen( variables.myXMLString.xmlRoot.Data.Item )#" index="i">
    #variables.myXMLString.xmlRoot.Data.Item[ variables.i ].xmlAttributes.description#
    </cfloop>
    this is how you access the array of items...

    Make sense???
    0
     

    Author Comment

    by:areola
    First off sorry to INSDivision6 for not replying to your request for more information sooner, I was away on a long weekend and didn't have a chance to reply.

    Secondly big thanks to Tacobell777 for understanding what I was whittering on about. Yes it makes perfect sense to me, it was how to reference the "xmlRoot.Data.Item" bit that was really throwing me.

    Also to anyone that finds this post later I inserted the cfhttp request with <cfhttp url="#urlAddress#" method="GET" timeout="15"> I then changed the first instance of "variables.myXMLString" to "cfhttp.filecontent" and later referneces to "objXML"

    Many thanks Tacobell777, I'm beginning to get my head round XML with Coldfusion, one big "A" to you!
    0
     
    LVL 17

    Expert Comment

    by:Tacobell777
    your welcome
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now