Kioko Kiaza
Kioko Kiaza

Reputation: 1398

Get all elements of an element

I've got this 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>[email protected]</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>

I'm trying to read this XML and insert the values into a mysql table (NAME,ADDRESS,SUBJECT,QUERYS). The problem is when I try to read for example a NAME field like this:

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)

The output with this code is "Pedrito De Rosa" instead of all the content . I mean, it should get all the P elements within "NAME" tags, otherwise we are losing data...

How can I get all the data within elements? I tried with record.findAll("NAME/P") but there is no findAll method.

Any help or clue?

I created a pyfiddle if anyone can help... https://pyfiddle.io/fiddle/9ed9743d-4d6e-4400-bfb5-19ba2bbf65f7/?i=true

thanks in advance

Upvotes: 0

Views: 56

Answers (4)

balderman
balderman

Reputation: 23815

Below

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>[email protected]</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]

Upvotes: 0

Usman
Usman

Reputation: 2029

Try this code. I choose to regex to get the name from XML.

Code :

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))

Output :

Name :  Pedrito De Rosa 

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With flexible element.xpath function:

...
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)

The output:

['Pedrito De Rosa', 'NIE X1111222233', 'tf 2283396922', '[email protected]']
['Paseo Jauregizahar 234 - 1. A. Donostia ']
['paisaje y ciudad ']
['2014-12-10 Avance Normas Subsidiarias', 'Otras consultas']

Upvotes: 1

ncica
ncica

Reputation: 7206

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)

output:

Pedrito De Rosa
NIE X1111222233
tf 2283396922
[email protected]

Upvotes: 1

Related Questions