的Python:指定xml.etree元素XMLNS

问题描述 投票:2回答:2

在我的Python代码我目前使用的xml.etree库创建一棵树,然后将其转储到一个XML字符串。不幸的是我不能使用比Python标准库之外的其他模块来做到这一点。

这里是我的代码:

import xml.etree.ElementTree as ET

def dump_to_XML():
  root_node = ET.Element("root")
  c1_node = ET.SubElement(root_node, "child1")
  c1_node.text = "foo"
  c2_node = ET.SubElement(root_node, "child2")
  gc1_node = ET.SubElement(c2_node, "grandchild1")
  gc1_node.text = "bar"
  return ET.tostring(root_node, encoding='utf8', method='xml')

这给字符串:

<?xml version='1.0' encoding='utf8'?>
<root>
    <child1>foo</child1>
    <child2>
        <grandchild1>bar</grandchild1>
    </child2>
</root>

现在,我有位于两个架构文件 - 说 - http://myhost.com/p.xsdhttp://myhost.com/q.xsd,我希望输出字符串变成:

<?xml version='1.0' encoding='UTF-8'?>
<root xmlns:p="http://myhost.com/p.xsd" xmlns:q="http://myhost.com/q.xsd">
    <p:child1>foo</p:child1>
    <p:child2>
        <q:grandchild1>bar</q:grandchild1>
    </p:child2>
</root>

我怎样才能充分利用etree库,以实现这一目标?

提前致谢

python schema xml-namespaces elementtree
2个回答
3
投票

开始了:

import xml.etree.ElementTree as ET

xmlns_uris = {'p': 'http://myhost.com/p.xsd',
              'q': 'http://myhost.com/q.xsd'}

def dump_to_XML():
  root_node = ET.Element("root")
  c1_node = ET.SubElement(root_node, "child1")
  c1_node.text = "foo"
  c2_node = ET.SubElement(root_node, "child2")
  gc1_node = ET.SubElement(c2_node, "grandchild1")
  gc1_node.text = "bar"
  annotate_with_XMLNS_prefixes(gc1_node, 'q', False)
  annotate_with_XMLNS_prefixes(root_node, 'p')
  add_XMLNS_attributes(root_node, xmlns_uris)
  return ET.tostring(root_node, encoding='UTF-8', method='xml')

def annotate_with_XMLNS_prefixes(tree, xmlns_prefix, skip_root_node=True):
    if not ET.iselement(tree):
        tree = tree.getroot()
    iterator = tree.iter()
    if skip_root_node: # Add XMLNS prefix also to the root node?
        iterator.next()
    for e in iterator:
        if not ':' in e.tag:
            e.tag = xmlns_prefix + ":" + e.tag

def add_XMLNS_attributes(tree, xmlns_uris_dict):
    if not ET.iselement(tree):
        tree = tree.getroot()
    for prefix, uri in xmlns_uris_dict.items():
        tree.attrib['xmlns:' + prefix] = uri

执行:print dump_to_XML()给:

<?xml version='1.0' encoding='UTF-8'?>
<root xmlns:p="http://myhost.com/p.xsd" xmlns:q="http://myhost.com/q.xsd">
    <p:child1>foo</p:child1>
    <p:child2>
        <q:grandchild1>bar</q:grandchild1>
    </p:child2>
</root>

0
投票
from lxml import etree
xmlns_uris = {'p': 'http://myhost.com/p.xsd', 'q': 'http://myhost.com/q.xsd'}

root = etree.Element('root', nsmap = xmlns_uris)
child1 = etree.SubElement(root,'{%s}child1'%xmlns_uris['p'])
child1.text = 'foo'

child2 = etree.SubElement(root,'{%s}child2'%xmlns_uris['p'])
grandchild1 = etree.SubElement(child2,'{%s}grandchild1'%xmlns_uris['q'])
grandchild1.text = 'bar'

print(etree.tostring(root, pretty_print=True, encoding='UTF-8', xml_declaration=True).decode('cp1251'))
© www.soinside.com 2019 - 2024. All rights reserved.