parse xml children with multiple children with same name, python

Peter0172

I have xml:

<tree-root>
<sub1>it is sub1</sub1>
    <sub2>
    <sub3>sub3</sub3>
    <position>5584</position>
    <source-IP>
    <address-IP>2.104.54.11</address-IP>
    <address-IP>172.16.33.11</address-IP>
   </source-IP>
</sub2>

I would like to parse all the children inside of source-IP. The output shall be both IPs not just one. I would like to have :

2.104.54.11
172.16.33.11

It is similar to Parse XML with Python when multiple children share a name, but I would kindly ask for all the code.

Thank you in advance.

I have tried print(tree.find('sub2').find('source-IP').findall('address-IP').text);, but only bringing the error:

'list' object has no attribute 'text'

Here is the full code:

print(tree.find('sub2').find('source-IP').find('address-IP').text)

{    import xml.etree.ElementTree as ET  
tree = ET.parse('IPs.xml')  
root = tree.getroot()


print(root.text)
print(tree.find('sub1').text)                         
print(tree.find('sub2').find('sub3').text)  
print(tree.find('sub2').find('position').text)
 print('I would like to print all the IPs below, not just the first one')
print(tree.find('sub2').find('source-IP').find('address-IP').text)
print(tree.find('sub2').find('source-IP').find('address-IP').text)
}
John Gordon

.findall() returns a list of items. You need to loop over each of those items to get the text:

for address_ip in tree.find('sub2').find('source-IP').findall('address-IP'):
    print address_ip.text

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get XML only immediate children elements by name

PHP SimpleXML - Multiple children with same name

Same style to multiple children

sql import xml file with multiple nodes/children

Jackson Faster XML: how to parse abstract class' children? Mixins?

How to access nested children with same name in XML using lxml in python

Python: large XML parsing with multiple children of root tree

How to use python parse json with nested children

SQL Server parse XML to table - multiple node with the same name

Parse xml to dataframe including children and attributes in R

XML in R: Multiple Children with Same Name without Loops

to parse xml node children values under same tagname

XML Schema Children With the Same Element

BeautifulSoup: Parse children by tag name

Adding Multiple Children of the Same Object As3

How to copy multiple files with a same name from children directory to another directory without losing the parent directory?

find and delete parent directories by the name without children directories with the same name

Parse multiple xml with the same input file with python

python JSON to dict check if single or multiple children

How to parse xml with varying number of children with rvest

Wrapping multiple children with same class name in multiple divs jQuery

PHP Iterate Multiple Children from XML

convert one parent and multiple identical children into xml

Python XML Sorting by Attribute/Children

Parsing xml with multiple children

Extracting Content when Multiple Children with Same Name (Google Scripts JS)

How to parse XML children using python

Parse XML file in python and retrieve nested children

How to iterate over XML children with same name as current element and avoid current element in iteration?