Solved

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

Posted on 2006-10-31
3
133 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

930 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

15 Experts available now in Live!

Get 1:1 Help Now