获取元素的所有元素

时间:2019-07-09 09:21:37

标签: python xml xml-parsing elementtree

我有这个XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <record ID="#046CE9401D01467B2BDBAF0" NumDoc="1461">
        <NAME>
            <P>Pedrito De Rosa</P>
            <P>NIE X1111222233</P>
            <P>tf 2283396922</P>
            <P>efael@hostmailer.com</P>
        </NAME>
        <ADDRESS>
            <P>Paseo Jauregizahar 234 &#45; 1&#46; A&#46; Donostia </P>
        </ADDRESS>
        <SUBJECT>
            <P>paisaje y ciudad </P>
        </SUBJECT>
        <QUERYS>
            <P>2014-12-10 Avance Normas Subsidiarias</P>
            <P>Otras consultas</P>
        </QUERYS>
    </record>
</root>

我正在尝试读取此XML并将值插入mysql表(NAME,ADDRESS,SUBJECT,QUERYS)。问题是当我尝试读取例如这样的NAME字段时:

from lxml import etree as ET

tree = ET.parse('data/data.xml')
root = tree.getroot()
records = tree.findall('record')
for i, record in enumerate(records):
    myname = record.find("NAME/P")
    print (myname.text)

此代码的输出为“ Pedrito De Rosa”,而不是所有内容。我的意思是,它应该将所有P元素都包含在“ NAME”标签中,否则我们将丢失数据...

如何获取元素中的所有数据?我尝试使用record.findAll(“ NAME / P”),但是没有findAll方法。

有帮助或线索吗?

如果有人可以帮助,我就创建了一个pyfiddle ... https://pyfiddle.io/fiddle/9ed9743d-4d6e-4400-bfb5-19ba2bbf65f7/?i=true

预先感谢

4 个答案:

答案 0 :(得分:1)

from lxml import etree as ET

tree = ET.parse('data.xml')
root = tree.getroot()
records = tree.findall('record')
for i, record in enumerate(records):
    myname = record.findall("NAME/P")

    for item in myname:
        print (item.text)

输出:

Pedrito De Rosa
NIE X1111222233
tf 2283396922
efael@hostmailer.com

答案 1 :(得分:1)

具有灵活的task manager功能:

element.xpath

输出:

...
root = tree.getroot()

records = tree.findall('record')
for i, record in enumerate(records):
    names = record.xpath("NAME/P/text()")
    print(names)

    addresses = record.xpath("ADDRESS/P/text()")
    print(addresses)

    subjects = record.xpath("SUBJECT/P/text()")
    print(subjects)

    querys = record.xpath("QUERYS/P/text()")
    print(querys)

答案 2 :(得分:0)

尝试此代码。 我选择使用正则表达式从XML中获取名称。

代码:

import re

line = "<NAME><P>Pedrito De Rosa</P></NAME>"
matchObj = re.search( r'.*NAME..P.(.*)..P...NAME', line, re.M|re.I)
if matchObj:
    print("Name : ", matchObj.group(1))

输出:

Name :  Pedrito De Rosa 

答案 3 :(得分:0)

下面

import xml.etree.ElementTree as ET

elements = ['NAME','ADDRESS','SUBJECT','QUERYS']
data = {}
xml = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <record ID="#046CE9401D01467B2BDBAF0" NumDoc="1461">
        <NAME>
            <P>Pedrito De Rosa</P>
            <P>NIE X1111222233</P>
            <P>tf 2283396922</P>
            <P>efael@hostmailer.com</P>
        </NAME>
        <ADDRESS>
            <P>Paseo Jauregizahar 234 &#45; 1&#46; A&#46; Donostia </P>
        </ADDRESS>
        <SUBJECT>
            <P>paisaje y ciudad </P>
        </SUBJECT>
        <QUERYS>
            <P>2014-12-10 Avance Normas Subsidiarias</P>
            <P>Otras consultas</P>
        </QUERYS>
    </record>
</root>'''

root = ET.fromstring(xml)
for e in elements:
  lst = root.find('.//record/{}'.format(e)).getchildren()
  data[e] =  [x.text for x in lst]