Skip to content Skip to sidebar Skip to footer

Get Text Inside Xml Tags By Their Name

I had a xml code and i want to get text in exact elements(xml tags) using python language . I have tried couple of solutions and didnt work. import xml.etree.ElementTree as ET tree

Solution 1:

Edited and improved answer:

import xml.etree.ElementTree as ET
import re

ns = {"veh": "http://schemas.conversesolutions.com/xsd/dmticta/v1"}

tree = ET.parse('test.xml') # save your xml as test.xml
root = tree.getroot()

def get_tag_name(tag):
    return re.sub(r'\{.*\}', '',tag)

for node in root.find(".//veh:return", ns):
    print(get_tag_name(node.tag)+': ', node.text)

It should produce something like this:

ResponseMessage:  None
ErrorCode:  None
RequestId:   2012290007705 
TransactionCharge:  150
VehicleNumber:  GF-0176
AbsoluteOwner:  SIYAPATHA FINANCE PLC
EngineNo:  GA15-483936F
ClassOfVehicle:  MOTOR CAR
Make:  NISSAN
Model:  PULSAR
YearOfManufacture:  1998
NoOfSpecialConditions:  0
SpecialConditions:  None

Post a Comment for "Get Text Inside Xml Tags By Their Name"