How can I check the existence of attributes and tags in XML before parsing?

Abhishek :

I'm parsing an XML file via Element Tree in python and and writing the content to a cpp file.

The content of children tags will be variant for different tags. For example first event tag has party tag as child but second event tag doesn't have.

-->How can I check whether a tag exists or not before parsing?

-->Children has value attribute in 1st event tag but not in second. How can I check whether an attribute exists or not before taking it's value.

--> Currently my code throws an error for non existing party tag and sets a "None" attribute value for the second children tag.

<main>
  <event>
    <party>Big</party>
    <children type="me" value="3"/>
  </event>

  <event>
    <children type="me"/>
  </event>

</main>

Code:

import xml.etree.ElementTree as ET
tree = ET.parse('party.xml')
root = tree.getroot()
for event in root.findall('event'):
    parties = event.find('party').text
    children = event.get('value')

I want to check the tags and then take their values.

Martijn Pieters :

If a tag doesn't exist, .find() indeed returns None. Simply test for that value:

for event in root.findall('event'):
    party = event.find('party')
    if party is None:
        continue
    parties = party.text
    children = event.get('value')

You already use .get() on event to test for the value the attribute; it returns None as well if the attribute does not exist.

Attributes are stored in the .attrib dictionary, so you can use standard Python techniques to test for the attribute explicitly too:

if 'value' in event.attrib:
    # value attribute is present.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check the existence of attributes and tags in XML before parsing?

How do I keep the html tags when parsing xml?

How can I clone a tag that have many other tags with Attributes?

How do I check for the existence of a variable

How I can use XML attributes to specify another XML name?

How can I check the load and existence of the module (Yii2)?

How can I check the existence of a language before set of its session in laravel?

Parsing XML with 'nil' attributes and empty 'element' tags using 'xmlbf'

How to access attributes of XML tags

Should I check object existence before selecting with jQuery?

How can I convert these tags and attributes to aspx.net?

How do I check for the existence of a bundle in twig?

How can I check for file existence in salt file server

How can I check for nested attributes in a Hash?

How can I check existence of object by Ebean?

check for multiple attributes existence

How do i Check if a xml Sibling has attributes?

How can I create Generics for XML Parsing?

How to check for existence of a pointer before delete in qt

Parsing xml with same tags and different attributes

How can I set attributes to li tags in CheckboxSelectMultiple?

How can I check the existence of tags in XML before parsing using ElementTree?

How can I check for existence of an object property?

How can I check the existence of several subfolders

How can I append XML parent tags to child tags?

Trying to check if a tag exists in XML before parsing

Python Beautiful Soup XML Conditional Query (mixed tags and attributes) Parsing

How can I sort XML elements by their attributes?

How can I check only existence in z3?

TOP Ranking

HotTag

Archive