Python Forum
xml.etree.ElementTree extract string values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml.etree.ElementTree extract string values
#1
Hi all,
I'am new to python. I am trying to parse an xml file and extract values between "><". Here the xml example :

<?xml version="1.0" encoding="UTF-8"?>
<DisplayDefinitionTable>
	<rows>
		<row>
			<object_tag tag="tagstr" uid="uidstr"/>
			<row_element column="0" component_tag="223011" property_name="property1">VALUE_STR_1</row_element>
			<row_element column="1" component_tag="223011" property_name="property2">VALUE_STR_2</row_element>
			<row_element column="2" component_tag="223011" property_name="property3">VALUE_STR_3</row_element>
			<row_element column="3" component_tag="223011" property_name="property4">VALUE_STR_4</row_element>
			<row_element column="4" component_tag="223011" property_name="property5">VALUE_STR_5</row_element>
			<row_element column="5" component_tag="1182129" property_name="property6">VALUE_STR_6</row_element>
			<row_element column="6" component_tag="81988" property_name="property7">VALUE_STR_7</row_element>
			<row_element column="7" component_tag="223011" property_name="property8">VALUE_STR_8</row_element>
			<row_element column="8" component_tag="223011" property_name="property9">VALUE_STR_9</row_element>
			<row_element column="9" component_tag="223011" property_name="property10">VALUE_STR_10</row_element>
		</row>
		
	</rows>
</DisplayDefinitionTable>[python]
[/python]

I'am trying to exrtract the value string for property1 between "><" (VALUE_STR_1) zu extrahieren.
Here the code :

from pathlib import Path
import os
import tempfile
import xml.etree.ElementTree as ET

srcpath = Path(__file__).parent.absolute()
os.chdir(srcpath)

tree = ET.parse("example.xml")
root = tree.iter()
#root = tree.getroot()

value= ""
PropertyName =""
for child in root:
     print(child.tag, child.attrib)
     if child.tag == "row_element":
        #print(child.tag,child.attrib)
        PropertyName=child.attrib.get('property_name')
        print('>>',PropertyName)
        value=child.findtext('PropertyName')
        print ("Value from ",PropertyName,":",value)
Attached the corresponding output :

DisplayDefinitionTable {}
rows {}
row {}
object_tag {'tag': 'tagstr', 'uid': 'uidstr'}
row_element {'column': '0', 'component_tag': '223011', 'property_name': 'property1'} 
>> property1
Value from  property1 : None
I did try various approaches but without success. I am under the impression the root element does not have those values at all. Any help or hint is highly appreciated

Thx
Matthias
Reply
#2
You can access text content simply with text attribute:
for child in root:
    print(child.tag, child.attrib)
    if child.tag == "row_element":
        # print(child.tag,child.attrib)
        PropertyName = child.attrib.get('property_name')
        print(f'>> {PropertyName}')
        value = child.text
        print(f'Value from {PropertyName}: {value}')
Reply
#3
Here one with BS as i never use ElementTree(has caused a lot of unnecessary problems for many people trough the years).
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('comp.xml'), 'xml')
for row in soup.find_all('row_element'):
    print(row.text)
Output:
VALUE_STR_1 VALUE_STR_2 VALUE_STR_3 VALUE_STR_4 VALUE_STR_5 VALUE_STR_6 VALUE_STR_7 VALUE_STR_8 VALUE_STR_9 VALUE_STR_10
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PDF Extract using CSV values atomxkai 5 154 Jan-13-2022, 12:20 PM
Last Post: Pedroski55
  Extract a string between 2 words from a text file OscarBoots 2 408 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  How to extract specific key value pair from string? aditi06 0 1,020 Apr-15-2021, 06:26 PM
Last Post: aditi06
Question How to extract multiple text from a string? chatguy 2 915 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Dataframe extract key values danipyth 0 662 Feb-07-2021, 03:52 PM
Last Post: danipyth
  Extract continuous numeric characters from a string in Python Robotguy 2 1,037 Jan-16-2021, 12:44 AM
Last Post: snippsat
  xml.etree.ElementTree question. water 0 1,308 Oct-09-2020, 06:47 PM
Last Post: water
  extract a dictionary from a string berc 4 1,329 Jul-30-2020, 06:58 AM
Last Post: berc
  Function parameters and values as string infobound 1 858 Jul-24-2020, 04:28 AM
Last Post: scidam
  Extract data from large string pzig98 1 1,039 Jul-20-2020, 12:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020