Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
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 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…

770 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