Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用BeautifulSoup替换/删除XML标记?_Python_Xml_Beautifulsoup - Fatal编程技术网

Python 如何用BeautifulSoup替换/删除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

我在一个本地文件中有XML,它是最后一条消息的模板,该消息被
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"))