Solved

Coldfusion and XML - need assistance reading XML values - am desperate!

Posted on 2006-10-31
3
131 Views
Last Modified: 2013-12-24
I'm afraid I have virtually no experience working with XML & Coldfusion, so any advice that anyone can provide would be great.  I am trying to grab a web page and get the certain values into a database.  The web page I am calling with <cfhttp> is a php file that returns contents as XML.

i.e.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE workspace PUBLIC "-//workname//DTD workspace 1.0" "http://localhost:8080/workspace/p.dtd">
<working type="d">
          <off><u>132915</u></off>
</working>
<working type="d">
    <on><u>132915</u></on>
</working >
<working type="d">
          <off><u>13112</u></off>
</working>

I am totally baffled as to how to accomplish getting the values for which numbers come across as <off></off> and which come across as <on></on> read out of the xml and into a usuable format.  If you need more information or I am not clear on this, please just let me know!

Thanks!
0
Comment
Question by:questhaven
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Marco_van_den_Oever earned 500 total points
ID: 17848861
Hi there, maybe it's easier to post the php page, it's very important that the page is formatted correctly and wrapped up correctly with tags (like: <item><grabthis></grabthis></item> and maybe exported to .xml format, then you should request the page like:

<!--- Retrieve the RSS document --->
<cfhttp url="http://wwwphppage.php" method="get">

<!--- Validation flag --->
<cfset XMLVALIDATION = true>

<cftry>
<!--- Create the XML object --->
<cfset objRSS = xmlParse(cfhttp.filecontent)>

<cfcatch type="any">
<!--- If the document retrieved in the CFHTTP
is not valid set the validation flag to false. --->
<cfset XMLVALIDATION = false>
</cfcatch>
</cftry>

<cfif XMLVALIDATION>
<!--- If the validation flag is true continue parsing --->

<!--- Set the XML Root --->
<cfset XMLRoot = objRSS.XmlRoot>

<!--- Retrieve the document META data --->
<cfset doc_grabthis = XMLRoot.channel.grabthis.xmltext>

<!--- Output the meta data in the browser --->

<!--- Retrieve the number of items in the channel --->
<cfset Item_Length = arraylen(XMLRoot.channel.item)>

<!--- Loop through all the items --->
<cfloop index="itms" from="1" to="#Item_Length#">
<!--- Retrieve the current Item in the loop --->
<cfset tmp_Item = XMLRoot.channel.item[itms]>

<!--- Retrieve the item data --->
<cfset grabthis = tmp_Item.grabthis.xmltext>

<!--- Output the items in the browser --->
<cfoutput>
#grabthis#
</cfoutput>
 . </cfloop>
<cfelse>
<!--- If the validation flag is false display error --->
Invalid XML/RSS object!
</cfif>

I'm not really a php guy, but if you need more advice and maybe post the code i will give it a try :)
0
 

Author Comment

by:questhaven
ID: 17861094
Thanks for your help Marco!  In the end I decided to write a parser to grab the data I needed - I  figured that I am not dealing with a huge amount of data anyway, so it just made more sense.  Here is the parser in case anyone else runs into this and wants to see an alternative:

<cfscript>
      onList = "";
      if (findNoCase("<on>", xmlString))
      {
            onList = removeChars(xmlString, 1, findNoCase("<on>", xmlString) + 6);
            onList = replaceNoCase(removeChars(onList, findNoCase("</on>", onList) - 4, len(onList)), "</u><u>", ",", "all");
      }
      
      offList = "";
      if (findNoCase("<off>", xmlString))
      {
            offList = removeChars(xmlString, 1, findNoCase("<off>", xmlString) + 7);
            offList = replaceNoCase(removeChars(offList, findNoCase("</off>", offList) - 5, len(offList)), "</u><u>", ",", "all");
      }
</cfscript>
0
 
LVL 2

Expert Comment

by:Marco_van_den_Oever
ID: 17861552
Hi there:) No problem, great to help and thanks for the example code, it's in my snippets folder now :)
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…

708 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

19 Experts available now in Live!

Get 1:1 Help Now