Python读写Berlekey DB XML的例子


  1. #!/usr/bin/env python
  2. # $Id: examples.py,v 1.23 2005/09/25 23:36:42 gmf Exp $
  3. # As of Python 2.3, there is a built-in module for Berkeley DB,
  4. # called bsddb.  The next uncommented line assumes that module is being used.
  5. # If you are using your own bsddb3 module, use this line:
  6. #      from bsddb3.db import *
  7. from bsddb3.db import *
  8. from dbxml import *
  9.  
  10. book_content = r"<book><title>Knowledge Discovery in Databases.</title></book>"
  11. book_content_ns = r"<book xmlns:books='http://foo.bar.com/books.dtd'><books:title>Knowledge Discovery in Databases.</books:title></book>"
  12. book_name = r"book1"
  13. book_name_ns = r"book1_ns"
  14.  
  15. def remove_database():
  16.     """Removes the databases so we can start with a fresh environment for
  17.     each test."""
  18.     import os, glob
  19.     databases = ["test", "test1", "test2"]
  20.     for database in databases:
  21.         files = glob.glob(database + "*.dbxml")
  22.         files += glob.glob("__db*")
  23.         files += glob.glob("log.*")
  24.         for file in files:
  25.             os.remove(file)
  26.  
  27. def example01():
  28.     """Create an XmlManager/XmlContainer, add a document, get the document,
  29.     display the content of the document.
  30.  
  31.     >>> remove_database()
  32.     >>> example01()
  33.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  34.     """
  35.     mgr = XmlManager()
  36.     uc = mgr.createUpdateContext()
  37.     container = mgr.createContainer("./test.dbxml")
  38.     container.putDocument(book_name, book_content, uc)
  39.     document = container.getDocument(book_name)
  40.     s = document.getContent()
  41.     print document.getName(), "=", s
  42.  
  43. def example02():
  44.     """Create an XmlManager/XmlContainer, add a document, query the container
  45.     for the document, iterate over the result set displaying the
  46.     values returned.
  47.  
  48.     >>> remove_database()
  49.     >>> example02()
  50.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  51.     """
  52.  
  53.     mgr = XmlManager()
  54.     uc = mgr.createUpdateContext()
  55.     container = mgr.createContainer("test.dbxml")
  56.     container.putDocument(book_name, book_content, uc)
  57.     qc = mgr.createQueryContext()
  58.     results = mgr.query("collection('test.dbxml')/book", qc)
  59.     results.reset()
  60.     for value in results:
  61.         document = value.asDocument()
  62.         print document.getName(), "=", value.asString()
  63.  
  64. def example03():
  65.     """Create an XmlContainer, add a document that includes a
  66.     namespace definition, create a query context, define a
  67.     namespace prefix to URI mapping, query the container
  68.     for the document within a context, iterate over the
  69.     result set displaying the values returned.
  70.  
  71.     >>> remove_database()
  72.     >>> example03()
  73.     book1_ns = <book xmlns:books="http://foo.bar.com/books.dtd"><books:title>Knowledge Discovery in Databases.</books:title></book>
  74.     """
  75.  
  76.     mgr = XmlManager()
  77.     uc = mgr.createUpdateContext()
  78.     container = mgr.createContainer("test.dbxml")
  79.     container.putDocument(book_name_ns, book_content_ns, uc)
  80.     qc = mgr.createQueryContext()
  81.     qc.setNamespace("books2", "http://foo.bar.com/books.dtd")
  82.     results = mgr.query("collection('test.dbxml')/*[books2:title='Knowledge Discovery in Databases.']", qc)
  83.     results.reset()
  84.     for value in results:
  85.         document = value.asDocument()
  86.         print document.getName(), "=", value.asString()
  87.  
  88. def example04():
  89.     """Create an XmlContainer, define an equality string index
  90.     for booktitle elements, add a document, query the container
  91.     for the document, iterate over the result set displaying
  92.     the values returned.
  93.  
  94.     Note that this example assumes that the 'test' container
  95.     does not already exist. If it does exist then the
  96.     addIndex method will throw an exception to complain
  97.     that the container isn't empty.
  98.  
  99.     Python note: This exception is not passed on to Python, which
  100.     will abort the interpreter.
  101.  
  102.     >>> remove_database()
  103.     >>> example04()
  104.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  105.     """
  106.  
  107.     mgr = XmlManager()
  108.     uc = mgr.createUpdateContext()
  109.     container = mgr.createContainer("test.dbxml")
  110.     container.addIndex("", "title", "node-element-equality-string", uc)
  111.     container.putDocument(book_name, book_content, uc)
  112.     qc = mgr.createQueryContext()
  113.     results = mgr.query("collection('test.dbxml')//*[title='Knowledge Discovery in Databases.']", qc)
  114.     for value in results:
  115.         document = value.asDocument()
  116.         print document.getName(), "=", value.asString()
  117.  
  118. def example05():
  119.     """
  120.     Create an XmlContainer, define an equality string index
  121.     for booktitle elements, add a document, create a query
  122.     context, define a variable binding, query the container
  123.     for the document within a context referencing the variable
  124.     defined, iterate over the result set displaying the values
  125.     returned.
  126.  
  127.     Note that this example assumes that the 'test' container
  128.     does not already exist. If it does exist then the
  129.     addIndex method will throw an exception to complain
  130.     that the container isn't empty.
  131.  
  132.     Python note: This exception is not passed on to Python, which
  133.     will abort the interpreter.
  134.  
  135.     >>> remove_database()
  136.     >>> example05()
  137.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  138.     """
  139.  
  140.     mgr = XmlManager()
  141.     uc = mgr.createUpdateContext()
  142.     container = mgr.createContainer("test.dbxml")
  143.     container.addIndex("", "title", "node-element-equality-string", uc)
  144.     container.putDocument(book_name, book_content, uc)
  145.     qc = mgr.createQueryContext()
  146.     results = mgr.query("collection('test.dbxml')//*[title='Knowledge Discovery in Databases.']", qc)
  147.     qc.setVariableValue("title", XmlValue("Knowledge Discovery in Databases."))
  148.     results = mgr.query("collection('test.dbxml')//*[title=$title]", qc)
  149.     for value in results:
  150.         document = value.asDocument()
  151.         print document.getName(), "=", value.asString()
  152.  
  153. def example06():
  154.     """Create an XML Container, rename that container, delete the
  155.     container, and repeat.
  156.  
  157.     >>> remove_database()
  158.     >>> example06()
  159.     """
  160.  
  161.     mgr = XmlManager()
  162.     for i in range(2):
  163.     container = mgr.createContainer("test1.dbxml")
  164.     del container
  165.         mgr.renameContainer("test1.dbxml", "test2.dbxml")
  166.         container = mgr.openContainer("test2.dbxml")
  167.     del container
  168.         mgr.removeContainer("test2.dbxml")
  169.  
  170. def example07():
  171.     """Create an use a transactional XML Container:
  172.     add a document, get the document, display the content of
  173.     the document.  Requires using DBEnv.
  174.  
  175.     >>> remove_database()
  176.     >>> example07()
  177.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  178.     """
  179.  
  180.     environment = DBEnv()
  181.     environment.open(None, DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN, 0)
  182.     mgr = XmlManager(environment, 0)
  183.     uc = mgr.createUpdateContext()
  184.     container = mgr.createContainer("test.dbxml", DBXML_TRANSACTIONAL)
  185.     xtxn = mgr.createTransaction()
  186.     container.putDocument(xtxn, book_name, book_content, uc)
  187.     xtxn.commit()
  188.     document = container.getDocument(book_name)
  189.     s = document.getContent()
  190.     print document.getName(), "=", s
  191.     container.close()
  192.     mgr.removeContainer("test.dbxml")
  193.     environment.close(0)
  194.  
  195. def example08():
  196.     """Create an XML Container within a Berkeley DB environment,
  197.     add a document, get the document, display the content of
  198.     the document.
  199.  
  200.     >>> remove_database()
  201.     >>> example08()
  202.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  203.     """
  204.  
  205.     environment = DBEnv()
  206.     environment.open(None, DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN, 0)
  207.     mgr = XmlManager(environment, 0)
  208.     uc = mgr.createUpdateContext()
  209.     container = mgr.createContainer("test.dbxml")
  210.     container.putDocument(book_name, book_content, uc)
  211.     document = container.getDocument(book_name)
  212.     s = document.getContent()
  213.     print document.getName(), "=", s
  214.     container.close()
  215.     mgr.removeContainer("test.dbxml")
  216.     environment.close(0)
  217.  
  218. def example09():
  219.     """Create an XML Container within a Berkeley DB environment,
  220.     then within a Berkeley DB transaction, add a document,
  221.     get the document, display the content of the document.
  222.     Use a Berkeley DB transaction
  223.  
  224.     >>> remove_database()
  225.     >>> example09()
  226.     book1 = <book><title>Knowledge Discovery in Databases.</title></book>
  227.     """
  228.  
  229.     environment = DBEnv()
  230.     environment.open(None, DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN, 0)
  231.     mgr = XmlManager(environment, 0)
  232.     uc = mgr.createUpdateContext()
  233.     container = mgr.createContainer("test.dbxml", DBXML_TRANSACTIONAL)
  234.     xtxn = mgr.createTransaction();
  235.     document = mgr.createDocument()
  236.     document.setContent(book_content)
  237.     document.setName(book_name)
  238.     container.putDocument(xtxn, document, uc)
  239.     document = container.getDocument(xtxn, book_name)
  240.     s = document.getContent()
  241.     print document.getName(), "=", s
  242.     xtxn.commit()
  243.     del document  # holds ref on container
  244.     del container # prevents removal
  245.     mgr.removeContainer("test.dbxml")
  246.     environment.close(0)
  247.  
  248. def example10():
  249.     """ Create two XML Containers within a Berkeley DB environment,
  250.     then within a Berkeley DB transaction add a document to
  251.     each container.
  252.  
  253.     >>> remove_database()
  254.     >>> example10()
  255.     """
  256.  
  257.     environment = DBEnv()
  258.     environment.open(None, DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN, 0)
  259.     mgr = XmlManager(environment, 0)
  260.     uc = mgr.createUpdateContext()
  261.     txn = environment.txn_begin()
  262.     xtxn = mgr.createTransaction(txn)
  263.     container1 = mgr.createContainer(xtxn, "test.dbxml")
  264.     container2 = mgr.createContainer(xtxn, "test2.dbxml")
  265.     container1.putDocument(xtxn, book_name, book_content, uc)
  266.     container2.putDocument(xtxn, book_name, book_content, uc)
  267.     txn.commit(0)
  268.  
  269.     del container1
  270.     del container2
  271.     del mgr
  272.     environment.close(0)
  273.  
  274. def example11():
  275.     """Demonstrate the definition of an index, and the iteration over set set
  276.     of indexes specified
  277.  
  278.     >>> remove_database()
  279.     >>> example11()
  280.     Indexes present:
  281.       http://www.sleepycat.com/2002/dbxml:name unique-node-metadata-equality-string
  282.       :title node-element-equality-date edge-element-equality-string
  283.     """
  284.  
  285.     mgr = XmlManager()
  286.     container = mgr.createContainer("test.dbxml")
  287.     uc = mgr.createUpdateContext()
  288.     """ addIndex uri, name, indexType updateContext"""
  289.     # the following (commented) addIndex call is equivalent to the
  290.     # 2 that follow it.
  291.     #container.addIndex("", "title", "node-element-equality-date, edge-element-equality-string", uc)
  292.     container.addIndex("", "title", XmlIndexSpecification.NODE_ELEMENT|
  293.                XmlIndexSpecification.PATH_NODE|
  294.                XmlIndexSpecification.KEY_EQUALITY, XmlValue.DATE, uc)
  295.     container.addIndex("", "title", XmlIndexSpecification.NODE_ELEMENT|
  296.                XmlIndexSpecification.PATH_EDGE|
  297.                XmlIndexSpecification.KEY_EQUALITY, XmlValue.STRING, uc)
  298.     # this is another way to add indexes..
  299.     #    ispec = container.getIndexSpecification()
  300.     #    ispec.addIndex("", "title", XmlIndexSpecification.NODE_ELEMENT|
  301.     #        XmlIndexSpecification.PATH_NODE|XmlIndexSpecification.KEY_EQUALITY,
  302.     #        XmlValue.DATE);
  303.     #    container.setIndexSpecification(ispec, uc)
  304.     n = 0
  305.     print "Indexes present:"
  306.     for index in container.getIndexSpecification():
  307.       print "  %s:%s %s" % (index.get_uri(), index.get_name(), index.get_index())
  308.     container.putDocument("foo", "<title>1957-11-03</title>", uc)
  309.     qc = mgr.createQueryContext()
  310.  
  311.     val = XmlValue(XmlValue.DATE, "1957-11-03")
  312.     res = container.lookupIndex(qc, "", "title", "node-element-equality-date",
  313.                 val)
  314.     # Shows how to look at a query plan
  315.     #print res.size()
  316.     #qe = mgr.prepare("collection('test.dbxml')/title[.<xs:date('1957-11-03')]", qc)
  317.     #print qe.getQueryPlan()
  318.    
  319.  
  320. def example12():
  321.     """Use WholedocContainer by setting default container type on XmlManager
  322.     >>> remove_database()
  323.     >>> example12()
  324.     document:  <book><title text='\\"'>content's  got &quot; dquot</title></book>
  325.     """
  326.     content = r"<book><title text='\"'>content's  got &quot; dquot</title></book>"
  327.     mgr = XmlManager()
  328.     uc = mgr.createUpdateContext()
  329.     mgr.setDefaultContainerType(XmlContainer.WholedocContainer)
  330.     container = mgr.createContainer("test.dbxml")
  331.     container.putDocument(book_name, content, uc)
  332.     doc = container.getDocument(book_name)
  333.     print "document: ", doc.getContent()
  334.  
  335.  
  336. # Test example of an overloade XmlResolver instance in Python
  337. # Used in example 13
  338. class myResolver(XmlResolver):
  339.     def __init__(self):
  340.     XmlResolver.__init__(self)
  341.     print "XmlResolver constructor"
  342.     def resolveDocument(self, txn, mgr, uri, result):
  343.     print "In resolveDocument, uri: %s" % (uri)
  344.     content=r"<?xml version='1.0' ?><root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://xxx mysch.xsd'><a>yyz</a></root>"
  345.     doc = mgr.createDocument()
  346.     doc.setContent(content)
  347.     newval = XmlValue(doc)
  348.     result.setValue(result, newval)
  349.     return 1
  350.     def resolveCollection(self, txn, mgr, uri, result):
  351.     print "In resolveCollection, uri: %s" % (uri)
  352.     return 0
  353.     def resolveSchema(self, txn, mgr, location, nameSpace):
  354.     print "In resolveSchema, location: %s" % (location)
  355.     return 0
  356.     def resolveEntity(self, txn, mgr, systemId, publicId):
  357.     print "In resolveEntity, systemId: %s" % (systemId)
  358.     dtd =r"<?xml version='1.0' encoding='utf-8' ?><!ELEMENT root (a)><!ELEMENT a (#PCDATA)>"
  359.     dlen = len(dtd)
  360.     istr = mgr.createMemBufInputStream(dtd, dlen, "dtd")
  361.     return istr
  362.  
  363.     def __del__(self):
  364.     XmlResolver.__del__(self)
  365.  
  366. def example13():
  367.     """Example of creating an XmlResolver instance in Python
  368.     >>> remove_database()
  369.     >>> example13()
  370.     XmlResolver constructor
  371.     In resolveEntity, systemId: mydtd.dtd
  372.     In resolveDocument, uri: myscheme:/myscheme:xxx
  373.     value of result:  <a>yyz</a>
  374.     """
  375.    
  376.     resolver = myResolver()
  377.     mgr = XmlManager(DBXML_ALLOW_EXTERNAL_ACCESS)
  378.     mgr.registerResolver(resolver)
  379.     uc = mgr.createUpdateContext()
  380.     container = mgr.createContainer("test.dbxml", DBXML_ALLOW_VALIDATION,
  381.                     XmlContainer.NodeContainer)
  382.     container.putDocument("foo0", r"<?xml version='1.0' ?><!DOCTYPE root SYSTEM 'mydtd.dtd'><root><a>yyz</a></root>", uc);
  383.     container.putDocument("foo", "<root/>", uc);
  384.     container.putDocument(book_name, book_content, uc)
  385.  
  386.  
  387.     # query to test the resolver
  388.     qc = mgr.createQueryContext()
  389.     qc.setBaseURI("myscheme:/")
  390.     results = mgr.query("doc('myscheme:xxx')/root/a", qc)
  391.     for value in results:
  392.         document = value.asDocument()
  393.         print "value of result: ", value.asString()
  394.  
  395. NUMBER_OF_EXAMPLES = 13
  396.  
  397. def do_example(number):
  398.     print "Running example %d." % number
  399.     globals()["example%02d" % number]()
  400.    
  401. if __name__ == "__main__":
  402.     import sys
  403.     number = sys.argv[-1]
  404.     if number == "test":
  405.         import doctest, examples
  406.         doctest.testmod(examples)
  407.         sys.exit()
  408.        
  409.     try:
  410.         number = int(number)
  411.     except ValueError:
  412.         print "Usage: ./examples.py <example_number>|test"
  413.         sys.exit()
  414.  
  415.     if number < 1 or number > NUMBER_OF_EXAMPLES:
  416.         print "Usage: ./examples.py <example_number>|test"
  417.         print "Example number out of range (1-%d)" % NUMBER_OF_EXAMPLES
  418.         sys.exit()
  419.  
  420.     remove_database()
  421.     do_example(number)

感谢您的关注。您现在可以 留言(0)留下通告地址



Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image