(very narrow) subset of xpath.
This example explicitly marshals/unmarshals the ElementTree document into
-mapped entities which have their own tables. Compare to pickle.py which uses
-pickle to accomplish the same task. Note that the usage of both styles of
-persistence are identical, as is the structure of the main Document class.
+mapped entities which have their own tables. Compare to pickle_type.py which
+uses PickleType to accomplish the same task. Note that the usage of both
+styles of persistence are identical, as is the structure of the main Document
+class.
"""
# PART I - Imports/Configuration
-import io
+from __future__ import print_function
+
import os
import re
from xml.etree import ElementTree
self.filename = name
self.element = element
- def __str__(self):
- buf = io.StringIO()
- self.element.write(buf)
- return buf.getvalue()
-
-
# PART IV - Persistence Mapping
# Node class. a non-public class which will represent the DB-persisted
n = _Node()
n.tag = str(node.tag)
n.text = str(node.text)
- n.tail = str(node.tail)
+ n.tail = str(node.tail) if node.tail else None
n.children = [traverse(n2) for n2 in node]
n.attributes = [
_Attribute(str(k), str(v)) for k, v in node.attrib.items()
print("\nFull text of document 'text.xml':", line)
document = session.query(Document).filter_by(filename="test.xml").first()
-print(document)
+ElementTree.dump(document.element)
# PART VI - Searching for Paths
.filter(and_(_Node.tag == "field1", _Node.text == "hi"))
.one()
)
-print(d)
+ElementTree.dump(d.element)
# generalize the above approach into an extremely impoverished xpath function:
# PART I - Imports/Configuration
-import io
+from __future__ import print_function
+
import os
import re
from xml.etree import ElementTree
from sqlalchemy.orm import Session
-e = create_engine("sqlite://", echo=True)
+e = create_engine("sqlite://")
meta = MetaData()
# PART II - Table Metadata
self.filename = name
self.element = element
- def __str__(self):
- buf = io.StringIO()
- self.element.write(buf)
- return buf.getvalue()
-
# PART IV - Persistence Mapping
print("\nFull text of document 'text.xml':", line)
document = session.query(Document).filter_by(filename="test.xml").first()
-print(document)
+ElementTree.dump(document.element)
# PART VI - Searching for Paths
.filter(and_(_Node.tag == "field1", _Node.text == "hi"))
.one()
)
-print(d)
+ElementTree.dump(d.element)
# generalize the above approach into an extremely impoverished xpath function: