繁体   English   中英

lxml,在元素之间获取xml

[英]lxml, get xml between elements

给出这个样本xml:

<xml>
<pb facs="id1" />
  <aa></aa>
  <aa></aa>
  <lot-of-xml></lot-of-xml>
<pb facs="id2" />
  <bb></bb>
  <bb></bb>
  <lot-of-xml></lot-of-xml>
</xml>

我需要解析它并获取pb之间的所有内容,保存到不同的外部文件中。

预期结果:

$ cat id1
  <aa></aa>
  <aa></aa>
  <lot-of-xml></lot-of-xml>

$ cat id2
  <bb></bb>
  <bb></bb>
  <lot-of-xml></lot-of-xml>

什么是正确的xpath斧使用?

from lxml import etree
xml = etree.parse("sample.xml")

for pb in xml.xpath('//pb'):
    filename = pb.xpath('@facs')[0]
    f = open(filename, 'w')

    content =  **{{ HOW TO GET THE CONTENT HERE? }}**

    f.write(content)
    f.close()

是否有任何xpath表达式来获取所有后代并在达到新的pb时停止?

好的,我测试了这段代码:

lists = []
for node in tree.findall('*'):
    if node.tag == 'pb':
        lists.append([])
    else:
        lists[-1].append(node)

输出:

>>> lists
[[<Element test at 2967fa8>, <Element test at 2a89030>, <Element lot-of-xml at 2a89080>], [<Element test at 2a89170>, <Element test at 2a891c0>, <Element lot-of-xml at 2a89210>]]

输入文件(以防万一):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<pb facs="id1" />
  <test></test>
  <test></test>
  <lot-of-xml></lot-of-xml>
<pb facs="id2" />
  <test></test>
  <test></test>
  <lot-of-xml></lot-of-xml>
</xml>

你想在两个pb之间提取标签吗? 如果是,那么这是不可能的,因为它不是pb之间的标签而不是与pb处于同一级别的单个标签,因为您关闭了标签pb。 如果在测试标记之后关闭标记,则测试可以成为pb的子级。

换句话说,如果你的xml是这样的:

<xml>
<pb facs="id1">
  <test></test>
</pb>
  <test></test>
<pb facs="id2" />
  <test></test>
  <test></test>
</xml>

然后你可以使用

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for child in root:
    for subchild in child:
        print subchild

以pb作为父项打印子项('test')。 好吧,如果不是这种情况(你只想提取pb标签的属性),那么你可以使用下面显示的两种方法中的任何一种来提取元素。 使用python的内置etree

import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
for child in root:
   if child.get('facs'):
       print child.get('facs')

使用lxml库,您可以像这样解析它:

tree = etree.parse('test.xml')
root = tree.getroot()
for child in root:
    if child.get('facs'):
        print child.get('facs')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM