简体   繁体   English

如何在Python中以XML添加前缀

[英]How to Add Prefix in XML in Python

I have an xml like this 我有一个这样的XML

<eo-gateway>
<interface-code>AAA</interface-code>
<supplier-code>XXX</supplier-code>
<authors>
<author type="first">
<forename>John</forename>
<surname>Smith</surname>
</author>
</authors>
</eo-gateway>

I need to arrive to this kind of xml adding prefix "art" in each tag. 我需要到达在每个标签中添加前缀“ art”的这种xml。

<art:eo-gateway>
<art:interface-code>AAA</art:interface-code>
<art:supplier-code>XXX</art:supplier-code>
<art:authors>
<art:author type="first">
<art:forename>John</art:forename>
<art:surname>Smith</art:surname>
</art:author>
</art:authors>
</art:eo-gateway>

Thanks for you help. 感谢您的帮助。

Use beautifulsoup : http://www.crummy.com/software/BeautifulSoup/bs4/doc/ 使用beautifulsoup: http : //www.crummy.com/software/BeautifulSoup/bs4/doc/

from bs4 import BeautifulSoup
soup = BeautifulSoup('''<eo-gateway>
<interface-code>AAA</interface-code>
<supplier-code>XXX</supplier-code>
<authors>
<author type="first">
<forename>John</forename>
<surname>Smith</surname>
</author>
</authors>
</eo-gateway>''')

for i in soup.find_all():
    i.name = 'art:' + i.name

And if you don't want some tags you could do this: 如果您不希望使用某些标签,可以执行以下操作:

except_these = ['art:body', 'art:html']

for i in soup.find_all():
    name = i.name
    if name not in except_these:
        i.name = 'art:' + i.name
print soup

Output: 输出:

<art:body>
<art:eo-gateway>
<art:interface-code>AAA</art:interface-code>
<art:supplier-code>XXX</art:supplier-code>
<art:authors>
<art:author type="first">
<art:forename>John</art:forename>
<art:surname>Smith</art:surname>
</art:author>
</art:authors>
</art:eo-gateway>
</art:body>

Or you could even check whether it already has 'art:' in front of it: 或者,您甚至可以检查它前面是否已经有“ art:”:

if !name.startswith('art:'):

I've already found the correct way to solve this using xml.etree that eliminates having extra tags like html and body tags from BeautifulSoup. 我已经找到了使用xml.etree解决此问题的正确方法,该方法消除了BeautifulSoup中的html和body标签等多余标签。

from xml.etree import ElementTree as etree

xml_content = """<eo-gateway>
    <interface-code>AAA</interface-code>
    <supplier-code>XXX</supplier-code>
    <authors>
    <author type="first">
    <forename>John</forename>
    <surname>Smith</surname>
    </author>
    </authors>
    </eo-gateway>"""

document_root = etree.ElementTree(etree.fromstring(xml_content))
for element in document_root.getiterator():
    element.tag = "art:" + element.tag
print etree.tostring(document_root.getroot())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM