コード例 #1
0
def xml_out(inventory,
            dest,
            instr=0,
            modified_after=None,
            stylesheet=None,
            indent=True):
    xinventory = _xmlwrap.xml_Inventory()

    _xmldoc_out(xinventory, inventory, instr, modified_after)

    if isinstance(dest, str):
        fd = open(dest, "wb")
    elif hasattr(dest, "write"):
        fd = dest
    else:
        raise TypeError("invalid file object")

    try:
        filename = fd.name
    except AttributeError:
        filename = '<???>'

    fd.write(b'<?xml version="1.0" encoding="utf-8"?>\n')

    if stylesheet is not None:
        fd.write(b'<?xml-stylesheet type="application/xml" href="%s"?>\n' % \
            (stylesheet,))

    if indent is True:
        _indent(xinventory._element)

    ET.ElementTree(xinventory._element).write(fd, encoding="utf-8")

    if isinstance(dest, str):
        fd.close()
コード例 #2
0
    def close(self):
        root = self.__p.close()
        if root.tag != _root_tag:
            raise DBError("unrecognized root element: " + root.tag)

        xinventory = _xmlwrap.xml_Inventory(root)
        _xmldoc_in(xinventory, self.__inventory)
コード例 #3
0
def xml_in(inventory, src):
    doc = ET.parse(src)
    root = doc.getroot()
    if root.tag != _root_tag:
        raise DBError("unrecognized root element: " + root.tag)

    xinventory = _xmlwrap.xml_Inventory(root)
    _xmldoc_in(xinventory, inventory)