在Python中使用ElementTree添加兄弟元素

4 投票
3 回答
3136 浏览
提问于 2025-04-16 12:26

有了这样的XML内容:

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

我需要添加一个兄弟节点,类似于:

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

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

3 个回答

-4

根据现有的内容:

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>"""
1

Python标准库里自带的ElementTree实现没有办法直接获取一个节点的父节点。

不过,如果你使用的是lxml的etree实现,那就有一个叫做getparent()的方法,可以用来获取父节点。

7

在标准库的版本中,你不能直接访问父节点。你需要从父节点开始往下找,或者自己记录父子关系。可以看看这些建议(这是这个库作者提供的)。

不过,如果你使用lxml,就有一个叫getparent()的方法(你还可以用getnext()getprevious()),而且更方便的是,还有addnext()addprevious()这两个方法。

所以,选择其中一个解决方案,取决于你使用的是哪个ElementTree的实现(或者你甚至可以考虑换一种实现)。

撰写回答