Python 如何用BeautifulSoup替换/删除XML标记?
我在一个本地文件中有XML,它是最后一条消息的模板,该消息被Python 如何用BeautifulSoup替换/删除XML标记?,python,xml,beautifulsoup,Python,Xml,Beautifulsoup,我在一个本地文件中有XML,它是最后一条消息的模板,该消息被POSTed发送到REST服务。脚本在发布模板数据之前对其进行预处理 因此,模板看起来像这样: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <singleElement> <subElementX>XYZ</subElementX> </singleEleme
POST
ed发送到REST
服务。脚本在发布模板数据之前对其进行预处理
因此,模板看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<singleElement>
<subElementX>XYZ</subElementX>
</singleElement>
<repeatingElement id="11" name="Joe"/>
<repeatingElement id="12" name="Mary"/>
</root>
我是否可以用其他内容替换重复元素,然后将soup对象转储到一个新字符串中,以便发布到restapi
注意:我使用的是html.parser
,因为我知道它可以正常工作,但理解html比XML解析更容易。您可以使用和方法:
然后,您可以使用
str(soup)
或soup.prettify()
转储“soup”。有趣的是,我刚刚同时提出了相同的解决方案。不幸的是,在我的系统(Win7)上唯一有效的soup解析器是html。解析器
(xml不按)将所有标记转换为小写,我的REST API区分大小写
xmlData = None
with open('conf//test1.xml', 'r') as xmlFile:
xmlData = xmlFile.read()
xmlSoup = BeautifulSoup(xmlData, 'html.parser')
repElemList = xmlSoup.find_all('repeatingelement')
for repElem in repElemList:
print("Processing repElem...")
repElemID = repElem.get('id')
repElemName = repElem.get('name')
# now I do something with repElemID and repElemName
# and no longer need it. I would like to replace it with <somenewtag/>
# and dump what is in the soup object back into a string.
# is it possible with BeautifulSoup?
for repElem in repElemList:
print("Processing repElem...")
repElemID = repElem.get('id')
repElemName = repElem.get('name')
repElem.replace_with(xmlSoup.new_tag("somenewtag"))