【问题标题】:Want to delete multiple tags with same tag name in XML using python elementtree?想要使用 python elementtree 删除 XML 中具有相同标签名称的多个标签?
【发布时间】:2013-06-20 14:06:19
【问题描述】:

我是 Python 新手,根据项目要求,我想针对不同的测试用例发起 Web 请求。假设(请参阅下面的 Employee_req.xml)对于一个测试用例,我想与所有组织一起启动 Web 服务。但对于另一个我想启动 Web 服务,其中所有名字标签都应该被删除。我在 python 中使用 ElementTree 来处理 XML。请在下面找到代码段。标签和属性值的修改可以正常工作,没有任何问题。但是在删除某些标签时,它会引发错误。我对 Xpath 不正确,请您提出可能的方法吗?

Emp_req.xml

<request>
    <orgaqnization>
        <name>org1</name>
        <employee>
            <first-name>abc</first-name>
            <last-name>def</last-name>
            <dob>19870909</dob>
        </employee>
    </orgaqnization>
    <orgaqnization>
        <name>org2</name>
        <employee>
            <first-name>abc2</first-name>
            <last-name>def2</last-name>
            <dob>19870909</dob>
        </employee>
    </orgaqnization>
    <orgaqnization>
        <name>org3</name>
        <employee>
            <first-name>abc3</first-name>
            <last-name>def3</last-name>
            <dob>19870909</dob>
        </employee>
    </orgaqnization>
</request>

Python::Test.py

modify_query("Remove",tag_name=".//first-name")
import xml.etree.ElementTree as query_xml
def modifiy_query(self,*args,**kwargs):   
        root = query_tree.getroot()         
        operation_type=args[0]
        tag_name=kwargs['tagname'] 
        try:              
            if operation_type=="Remove":    
                logger.info("Removing %s Tag from XML" % tag_name)
                root.remove(tag_name)               
            elif operation_type=="Insert":                        
                logger.info("Inserting %s tag to xml" % tag_name)
            else:
                raise InvalidXMLOperationError("Operation " + operation_type + " is invalid")
        except InvalidXMLOperationError,e:
            logger.error("Invalid XML operation %s" % operation_type)

The error message (Flow could be differ because i am running this code from some other program):

    File "Test.py", line 161, in <module> testsuite.scheduler() 
    File "Test.py", line 91, in scheduler self.launched_query_with("Without_date_range") 
    File "Test.py", line 55, in launched_query_with test.modifiy_query("Remove",tagname='.//first-name') 
    File "/home/XXX/YYYY/common.py", line 287, in modifiy_query parent.remove(child) 
    File "/usr/local/lib/python2.7/xml/etree/ElementTree.py", line 337, in remove self._children.remove(element) 
    ValueError: list.remove(x): x not in list

谢谢,

普里扬克·沙阿

【问题讨论】:

  • 能否提供错误信息?
  • 这是 ValueError: ValueError: list.remove(x): x not in list
  • 如果您将整个消息粘贴到问题中会更容易。
  • File "Test.py", line 161, in &lt;module&gt; testsuite.scheduler() File "Test.py", line 91, in scheduler self.launched_query_with("Without_date_range") File "Test.py", line 55, in launched_query_with test.modifiy_query("Remove",tagname='.//dateRange') File "/home/XXX/YYYY/common.py", line 287, in modifiy_query parent.remove(child) File "/usr/local/lib/python2.7/xml/etree/ElementTree.py", line 337, in remove self._children.remove(element) ValueError: list.remove(x): x not in list
  • 我通过这样做解决了这个问题:: tag_name=kwargs['tagname'] logger.info("Removing %s Tag from XML" % tag_name) parentlist=root.findall(".//*") for parent in parentlist: child=parent.find(tag_name) if child is not None: parent.remove(child)

标签: python xml elementtree


【解决方案1】:

remove 将元素作为参数,而不是 xpath。

代替:

root.remove(tag_name)

你应该有:

elements = root.findall(tag_name)
for element in elements:
    root.remove(element)  

【讨论】:

  • 这仍然不起作用。下面是我所做的代码:我正在调用这个函数,例如 :: Test.modifiy_query("Remove",tagname='first-name') tag_name=kwargs['tagname'] logger.info("Removing %s Tag from XML" % tag_name) childnodes=root.findall(tag_name) print len(childnodes) for child in childnodes: root.remove(child)
  • 感谢 morphyn.. 我通过调整逻辑解决了这个问题:感谢您的时间...tag_name=kwargs['tagname'] logger.info("Removing %s Tag from XML" % tag_name) parentlist=root.findall(".//*") for parent in parentlist: child=parent.find(tag_name) if child is not None: parent.remove(child)
猜你喜欢
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2023-04-09
  • 2023-03-06
  • 2015-09-25
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多