The find method can accept some limited Xpath expressions, you can use this to extract only IPs which are marked as Primary:

from xml.etree import ElementTree
tree = ElementTree.fromstring(sample)

for node in tree.iter('Host'):
    hostname = node.find('Name').text
    ips = node.findall("Networking[Primary='Yes']/IP")
    print hostname
    for ip in ips:
        print ip.text

For further information on what XPath expressions are allowed see the documentation at: https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element


The sample XML provided in the question is malformed in a couple of areas (presumably when it was obfuscated for posting, or the code example given could never have worked). The Type tag is closed twice, and the Primary tags are mismatched with closing Weight tags

Answer from James on Stack Overflow
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
Other parsing functions may create an ElementTree. Check the documentation to be sure. As an Element, root has a tag and a dictionary of attributes: ... >>> for child in root: ... print(child.tag, child.attrib) ... country {'name': 'Liechtenstein'} country {'name': 'Singapore'} country {'name': 'Panama'} Children are nested, and we can access specific child nodes by index: ... Not all elements of the XML input will end up as elements of the parsed tree.
🌐
TutorialKart
tutorialkart.com › python › python-xml-parsing
XML Parsing in Python
April 5, 2023 - # Python XML Parsing import xml.etree.ElementTree as ET root = ET.parse('sample.xml').getroot() # iterate over child nodes for holiday in root.findall('holiday'): # get all attributes of a node attributes = holiday.attrib print(attributes) # get a particular attribute type = attributes.get('type') print(type)
🌐
Stack Overflow
stackoverflow.com › questions › 38002686 › parsing-multiple-child-elements-in-python
xml - Parsing multiple child elements in python - Stack Overflow
Here i will use standart python library, but you also should look at other librarys. Most popular, as far as i know, are lxml, BeautifulSoup. ... Copyimport xml.etree.ElementTree as ET tree = ET.parse( 'test.xml' ) root = tree.getroot() all_items = [] for node in root.findall( 'sotransitem' ): item = Sotransitem() item.recordno = int( node.find( 'recordno' ).text ) item.unit = node.find('unit').text for custom_node in node.findall('./customfields/customfield'): value = custom_node.find('customfieldvalue').text name = custom_node.find('customfieldname').text item.customfields[ name ] = value all_items.append( item ) print( all_items ) # [Item( rec_no: 40562, fields: {'TEST_NUMBER': None, 'TESTCUSTOM': 'true'} ), Item( rec_no: 40563, fields: {'TESTCUSTOM': 'true', 'NUMBER1': None} )
🌐
Stack Overflow
stackoverflow.com › questions › 66555979 › parsing-xml-with-multiple-children
python - Parsing xml with multiple children - Stack Overflow
import pandas as pd import xml.etree.ElementTree as et xtree = et.parse("file.xml") namespaces = {'myprefix':'http:...'} # <-- this is from <pricebooks xmlns="http:..."> df_cols = ["product-id", "quantity", "amount", "price-info"] rows = [] for row in xtree.findall('.//myprefix:price-table[@product-id]', namespaces): price_info = row.find('.//myprefix:price-info', namespaces) if not price_info is None: price_info = price_info.text for amount in row.findall('.//myprefix:amount[@quantity]', namespaces): rows.append(dict(zip(df_cols, [row.attrib.get('product-id'), amount.attrib.get('quantity'), amount.text, price_info]))) df = pd.DataFrame(rows) print(df)
🌐
Theaccountant
theaccountant.org.mt › 2010-equinox-i0unr › parsing-xml-with-multiple-child-nodes-in-python.html
Parsing xml with multiple child nodes in python
Loop through all the nodes and for each nodes get the child Parsers are represented by parser objects. setAttributeNode rel 'Save the XML file XDoc. If there are multiple children with the same name, slicing and indexing can be used. The XML module will be used for dealing with the data and ...
Find elsewhere
🌐
Real Python
realpython.com › python-xml-parser
A Roadmap to XML Parsers in Python – Real Python
September 25, 2023 - This non-blocking incremental parsing strategy allows for a truly concurrent parsing of multiple XML documents on the fly while you download them. Elements in the tree are mutable, iterable, and indexable sequences. They have a length corresponding to the number of their immediate children:
🌐
Medium
medium.com › @jasonclwu › xml-parsing-with-python-united-nations-security-council-consolidated-list-part-3-2c4e4fb47802
XML Parsing with Python: United Nations Security Council Consolidated List (Part 3) | by Jason Wu | Medium
August 1, 2022 - DATAID 110404 has 3 child-nodes under DESIGNATION and they will be captured as 3 rows in the stacked dataframe. The original XML for DATAID 110404 is listed below for reference.
🌐
Stack Overflow
stackoverflow.com › questions › 69511153 › python-parsing-multiple-xml-nodes-with-dynamic-data
Python Parsing Multiple XML Nodes with Dynamic Data - Stack Overflow
Copyimport xml.etree.ElementTree as ET def parseRequestMeta(RequestMeta): """Parse your interest here """ for root in RequestMeta: print(root.tag) for child in root.iter(): print(child.tag, child.text) def parseRequest(Request): psss def parseReplyMeta(ReplyMeta): psss def parseReply(Reply): psss RequestMeta = [] Request = [] ReplyMeta = [] Reply = [] events = ["start", "end"] for event, node in ET.iterparse('trace.xml', events=events): if event == "end" and node.tag == "RequestMeta": RequestMeta.append(node) print(node.tag) if event == "end" and node.tag == "Request": Request.append(node) print(node.tag) if event == "end" and node.tag == "ReplyMeta": ReplyMeta.append(node) print(node.tag) if event == "end" and node.tag == "Reply": Reply.append(node) print(node.tag) parseRequestMeta(RequestMeta) parseRequestMeta(Request) parseRequestMeta(ReplyMeta) parseRequestMeta(Reply)
🌐
Stack Overflow
stackoverflow.com › questions › 70853332 › parsing-xml-via-python-when-i-have-the-same-child-names
parsing xml via python: when I have the same child names - Stack Overflow
and is there a way to avoid using range(2) because in my original code I have multiple intervals. – Royal Bhandari Commented Jan 25, 2022 at 18:03 ... You should iterate in inner loop not using root, but selected element. This code should work: xml ='''<meandata> <interval begin="0.00" ...
🌐
Stack Overflow
stackoverflow.com › questions › 67687755 › parsing-xml-for-sub-children-using-using-python
Parsing XML for sub children using using python - Stack Overflow
import requests import xml.etree.ElementTree as ET tree = ET.parse("out.xml") root = tree.getroot() for child in root.find('./layer3Sections'): print(child.tag, child.attrib)
🌐
Stack Overflow
stackoverflow.com › questions › 40583088 › python-how-to-parse-a-xml-with-dynamic-number-of-children-nodes
Python: How to parse a XML with dynamic number of children nodes? - Stack Overflow
November 14, 2016 - There might be other such nodes with a lot more types of children nodes so manually writing if...elif doesn't seem feasible. ... xml_str = ''' <cars> <car type="A" value="32"/> <car type="B" value="42"/> <car type="C" value="55"/> <car type="D" value="23"/> </cars> ''' import xml.etree.ElementTree as ET root = ET.fromstring(xml_str) cars = {} for child in root: cars[child.attrib['type']] = child.attrib['value']
Top answer
1 of 1
1

In ElementTree model, text node that comes after (following sibling of) an element is stored as tail of that element, not text of the parent element. So besides section.text, you also need to look into section.tail :

>>> section in abstract:
...     print section.text.strip()
...     if section.tail:
...         print section.tail.strip()
... 

Abstract

Amphinomids, more commonly known as fireworms, are a basal lineage of marine annelids characterized by the presence of defensive dorsal calcareous chaetae, which break off upon contact. It has long been hypothesized that amphinomids are venomous and use the chaetae to inject a toxic substance. However, studies investigating fireworm venom from a morphological or molecular perspective are scarce and no venom gland has been identified to date, nor any toxin characterized at the molecular level. To investigate this question, we analyzed the transcriptomes of three species of fireworms—

Eurythoe complanata
,
Hermodice carunculata
, and
Paramphinome jeffreysii
—following a venomics approach to identify putative venom compounds. Our venomics pipeline involved de novo transcriptome assembly, open reading frame, and signal sequence prediction, followed by three different homology search strategies: BLAST, HMMER sequence, and HMMER domain. Following this pipeline, we identified 34 clusters of orthologous genes, representing 13 known toxin classes that have been repeatedly recruited into animal venoms. Specifically, the three species share a similar toxin profile with C-type lectins, peptidases, metalloproteinases, spider toxins, and CAP proteins found among the most highly expressed toxin homologs. Despite their great diversity, the putative toxins identified are predominantly involved in three major biological processes: hemostasis, inflammatory response, and allergic reactions, all of which are commonly disrupted after fireworm stings. Although the putative fireworm toxins identified here need to be further validated, our results strongly suggest that fireworms are venomous animals that use a complex mixture of toxins for defense against predators.