【问题标题】:Adding sibling element in ElementTree with Python使用 Python 在 ElementTree 中添加兄弟元素
【发布时间】:2011-02-24 01:53:49
【问题描述】:

用XML如

<a>
  <b>
  </b>
</a>

我需要添加类似的兄弟姐妹

<a>
  <b>
  </b>
  <b'>
  </b'>
</a>

ElementTree 有添加兄弟节点的功能吗? 如果没有,我想我需要一个函数来获取父节点并添加子节点,我该怎么做?

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

    在标准库的版本中,您不能直接访问父级,您必须从父级向下工作,或者自己跟踪父子关系,请阅读these tips(来自库的作者)。

    如果您使用lxml,则有getparent() 方法(您也有getnext()getprevious()),但更方便的是:有addnext()addprevious()

    因此,根据您使用的 ElementTree 实现选择其中一种解决方案(或者甚至切换您的实现)

    【讨论】:

      【解决方案2】:

      Python 标准库中包含的默认 ElementTree 实现没有获取父节点的方法。

      但是,如果您可以使用lxml's etree 实现,那么有一个getparent() 方法。

      【讨论】:

        【解决方案3】:

        手头有什么:

        import re
        
        def sibling(bal,text):
            print 'bal ==' + bal + '\n' # + '\n\n' + s
        
            def aux(match):
                # match.group(4) is '/' or None    
                return ("%s%s<%s'/>%s" % match.group(1,2,3,6) if match.group(4)
                        else "%s%s<%s'>%s'>%s" % match.group(1,2,3,5,6))
        
            return re.sub('('
                          '(^\s*)<(' + bal + ')(?: [^/>]+)?'  # 2 and 3
                          '(?:(/)|>(?:.*?)((?:(?:\n|\r\n?)\\2)?</\\3))' # 4 and 5
                          '>(\n|\r\n?|\Z)' # 6
                          ')',
                          aux, text, flags = re.MULTILINE|re.DOTALL)
        
        
        ch = """\
        <a>
          <b>
          </b>
        </a>
        """
        
        dh = """\
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        """
        
        print sibling('a',ch)
        print '------------------------------------------------------'
        print sibling('b',ch)
        print '------------------------------------------------------'
        
        for x in ('entry','dd:country_code','content','OTF','FECS',
                  'BackEndCompatibility','BackEnd','Forth'):
            print sibling(x,dh)
            print '------------------------------------------------------'
        

        结果:

        bal ==a
        
        <a>
          <b>
          </b>
        </a>
        <a'>
        </a'>
        
        ------------------------------------------------------
        bal ==b
        
        <a>
          <b>
          </b>
          <b'>
          </b'>
        </a>
        
        ------------------------------------------------------
        bal ==entry
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        <entry'>
        </entry'>
        
        ------------------------------------------------------
        bal ==dd:country_code
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <dd:country_code'></dd:country_code'>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        bal ==content
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <content'></content'>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        bal ==OTF
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
            <OTF'>
            </OTF'>
        </entry>
        
        ------------------------------------------------------
        bal ==FECS
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
              <FECS'>
              </FECS'>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        bal ==BackEndCompatibility
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
                <BackEndCompatibility'>
                </BackEndCompatibility'>
              </FECS>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        bal ==BackEnd
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <BackEnd'/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        bal ==Forth
        
        <?xml version="1.0" encoding="utf-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
        <entry>
            <dd:country_code>USA</dd:country_code>
            <content type="html">Hello World!</content>
            <OTF>
              <FECS state="disabled" version="2.2.0.0">
                <BackEndCompatibility major="2.2" state="disabled">
                    <BackEnd state="disabled" version="2.2.0.0"/>
                    <Forth type="ziwi">d,;%er*dkj@jkber-uyr</Forth>
                    <Forth'></Forth'>
                </BackEndCompatibility>
              </FECS>
            </OTF>
        </entry>
        
        ------------------------------------------------------
        

        它适用于'a'

        """\
        <a>
          <b>
          </b>
        </a>
        """
        

        但不是

        """\
        <a>
          <b>
          </b>
        </a>"""
        

        【讨论】:

        猜你喜欢
        • 2020-11-15
        • 2013-12-04
        • 2020-03-24
        • 2012-07-15
        • 2019-08-29
        • 2022-01-17
        • 1970-01-01
        • 2023-02-09
        • 1970-01-01
        相关资源
        最近更新 更多