I am using ColdFusion (openBlueDragon) to insert the data from a large (200MB) xml file into a database without having to load the entire file into memory which is how I traditionally would do it. I did find a VERY SIMILAR QUESTION here: Looping over a large XML file that seems to be the answer I am looking for.
However, I’m not skilled enough in java to understand and adapt the code to my needs. I found no way to respond to the expert (@orangepips) who posted the code or else I would not have posted such a similar question.
My xml file looks like this:
<allItems>
<item>
<subject>The subject text</subject>
<date>2007-05-21 04:03:00</date>
<content>text content often contains many paragraphs of text</content>
<author>JPass78</author>
</item>
</allItems>
This is the code, courtesy orangepips, that I’m trying to adapt for my purpose. I’ve modified it a bit to include my own field names:
<cfset fis = createObject("java", "java.io.FileInputStream").init(
"#getDirectoryFromPath(getCurrentTemplatePath())#/file.xml")>
<cfset bis = createObject("java", "java.io.BufferedInputStream").init(fis)>
<cfset XMLInputFactory = createObject("java", "javax.xml.stream.XMLInputFactory").newInstance()>
<cfset reader = XMLInputFactory.createXMLStreamReader(bis)>
<cfloop condition="#reader.hasNext()#">
<cfset event = reader.next()>
<cfif event EQ reader.START_ELEMENT>
<cfswitch expression="#reader.getLocalName()#">
<cfcase value="allItems">
<!--- root node, do nothing --->
</cfcase>
<cfcase value="item">
<!--- set values used later on for inserts, selects, updates --->
</cfcase>
<cfcase value="subject">
<!--- some selects and insert --->
</cfcase>
<cfcase value="contentdate">
<!--- insert or update --->
</cfcase>
<cfcase value="content">
</cfcase>
<cfcase value="author">
</cfcase>
</cfswitch>
</cfif>
</cfloop>
<cfset reader.close()>
I have a single table and I am trying to figure out how do I access the values from each XML element so I may insert it one row at a time? like this: INSERT INTO content (subject,contentdate, content, author)
VALUES (“The subject text”, 2007-5-21 04:03:00, “text content here”,”JPass78″);
Instead of using COLDFUSION to import large XML files into a MYSQL database, use the MYSQL command “LOAD XML INFILE”.
Here’s the simple, light and fast code that worked for me:
LOAD XML INFILE 'pathtofile/file.xml' INTO TABLE table_name ROWS IDENTIFIED BY '<item>';My xml file uses the same exact field names as my database table. ROWS IDENTIFIED BY tells the command that the field names in my xml file will correspond to the database fields in my table and they will be found in between the <item></item> tags.
FYI, <item> is my own naming format. Your file will likely have another tag name that relates to the data you’re working with. For example, if your xml file is for employee data, you might instead use <employee>
Available in MYSQL5.5 – Reference for LOAD XML INFILE can be found at:
http://dev.mysql.com/doc/refman/5.5/en/load-xml.html