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()

defget_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:NoneErrorCode:NoneRequestId:2012290007705TransactionCharge:150VehicleNumber:GF-0176AbsoluteOwner:SIYAPATHAFINANCEPLCEngineNo:GA15-483936FClassOfVehicle:MOTORCARMake:NISSANModel:PULSARYearOfManufacture:1998NoOfSpecialConditions:0SpecialConditions:None

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