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");