Sign In/My Account | View Cart  
advertisement

Article:
 Scaling Up with XQuery, Part 2
Subject: Easier way to put an XML file into Berkeley DB XML
Date: 2006-06-28 14:37:45
From: dschachter

In your article at http://www.xml.com/pub/a/2006/06/21/scaling-up-with-xquery-part-2.html?page=2, you present Python code to load documents into a Berkeley DB XML database. Having recently delivered Berkeley DB XML training to Oracle employees, I'd like to offer a couple of comments:


The Python code


for filename in recipeFilenames:


fileObject = open(recipePath + filename + ".xml")
fileContents = r""


for line in fileObject:
fileContents = fileContents + line


container.putDocument(filename, fileContents, uc)


fileObject.close()



could be slightly more concise:


for filename in recipeFilenames:


fileObject = open(recipePath + filename + ".xml")
fileContents = fileObject.read()


container.putDocument(filename, fileContents, uc)


fileObject.close()


Were I to write the Python code, I'd choose a code layout to minimize the chance of memory leaks and unwanted variable references:


for filename in recipeFilenames:


fileObject = open(recipePath + filename + ".xml")
container.putDocument(filename, fileObject.read(), uc)
fileObject.close()



More importantly, Berkeley DB XML has a method you can use to load a file into a container directly,


XmlInputStream XmlManager::createLocalFileInputStream(const std::string & filename) const)


In C++ code, one would use this with the container method


std::string XmlContainer::putDocumentXml(const std::string &name,
XmlInputStream *adopted_input,
XmlUpateContext & context,
u_int32_t flags = 0)


These methods make it unnecessary for your code to open, read, and close files (and URLs, which I use in my demos).


-- David Schachter


No Previous Message Previous Message   Next Message No Next Message


Titles Only Titles Only Newest First

Sponsored By: