Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML

Status
Not open for further replies.

laurent42

Programmer
Aug 7, 2001
13
US
Hi,

I would like to parse an XML file with ColdFusion 4 after it has been uploaded to a web server.

I need to compare its contents with existing data in a database and offer to the user the choice to replace the data in the database by what is in the XML file.

All this in two .cfm pages if possible ie the first one for the upload, the parsing and the display of the XML data vs. the database's data and the second one to process the user's decision to replace or not the data in the database by the data in the XML file.

Please point me towards any example or similar threads.

Thanks.
 
Using XML got quite a bit easier with ColdFusion MX. Your options are somewhat limitted if you're still on 4 (are you at least running 4.5??).

You could certainly just use CFFILE to read in the XML, and do all the parsing yourself. I think you'll find that somewhat tedious and frustrating.

One option (that may or may not work with CF 4... can't remember) is the XML Toolkit that Tom Dyson maintains at ... it's a full set of custom tags and samples that help out a great deal (at least they did in CF 5).

He also maintains a mailing list (available at that same URL) concerning things CF/XML. It isn't particularly active, but it's ultra-responsive when someone poses a question. And I've seen a lot of useful info.


I don't mean to keep harping on it... but just in case you weren't aware... CF MX has built-in XML handling. Pretty much call two native tags and your entire XML file is suddenly parsed into a very cool, easy-to-use structure. One more call and it's automatically transformed according to an XSLT. Very cool. I've had clients, who weren't ready to fully upgrade their regular web servers to MX, decide it was worth setting up a subsystem on a separate server just to be able to use the XML handling as a web service.


-Carl
 
I have MX on the webserver.

What are the tags I have to use to read the XML file once it is uploaded so I can compare its contents with data that is in a database.

The XML file could have up to 50 "objects" from one kind and 3 "objects" from another kind.

Thanks.
 
You use CFFILE to read the XML file into a string variable... ie:
Code:
<CFFILE action=&quot;READ&quot; file=&quot;/mypath/myXMLfile.xml&quot; variable=&quot;sXMLstring&quot;>

Then you use the XMLParse() function to turn that string into an &quot;XML object&quot;.
Code:
<CFSET xmlMyXML = XMLParse(sXMLstring)>

Once you have it as an XML object, you can use XMLChildPos() and/or XMLSearch to find elements... or you can actually treat the XML object almost as a standard structure and address elements that way, if your XML data is set up to allow it.

For example, lets say I had an XML file like:
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<CatPage>
    <Meta>
      <Title>Catalog Page - Shoes</Title>
      <Keywords>sneakers,tennis shoes,casual</Keywords>
      <Description>Catalog - Shoes Home Page</Description>
      <Product>1234,3543,6543,7654</Product>
      <DocType>General, Catalog</DocType>
      <LastModified>20030803</LastModified>
    </Meta>
    <Nav>
      <Template>Leaf</Template>
      <Printable>no</Printable>
      <Header>Shoes</Header>
      <Tabs>Tennis,Cross-trainers,Sandals</Tabs>
    </Nav>
</CatPage>

I can get at the Title element by simply looking at:
Code:
#xmlMyXML.CatPage.Meta.Title.XMLText#

Of course... this is only possible because there is only one Title element under Meta, etc. ... which is, admittedly, pretty unusual for an XML document. But it's there.

In most cases you'll have to use XMLSearch to do XMLPath-type searches.



-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top