Get cell name from XML file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Get cell name from XML file (/thread-36621.html) |
Get cell name from XML file - paulo79 - Mar-10-2022 Hi all, I have an oracle table with CLOB column type with this content below: Quote:<?xml version="1.0" encoding="utf-8" ?> How can I create a python program to get <cell> <name> from that column in oracle table ? RE: Get cell name from XML file - Gribouillis - Mar-11-2022 You already know how to parse XML data, as this thread shows. Just do this with the content extracted from the database. RE: Get cell name from XML file - paulo79 - Mar-11-2022 (Mar-11-2022, 08:10 AM)Gribouillis Wrote: You already know how to parse XML data, as this thread shows. Just do this with the content extracted from the database. In the other thread I read a XML file. I want to read direct from Oracle table and not parse XML file. Is there any example? Tks RE: Get cell name from XML file - DeaD_EyE - Mar-11-2022 I copied your content to doc.xml. Example with a real file: import xml.etree.ElementTree as ET root_doc = ET.parse("doc.xml").getroot() # find first (one) match1 = root_doc.find("cell/name") if match1 is not None: match1_text = match1.text print(match1_text) # find all # if nothing was found, an empty list is returned # by findall matches = root_doc.findall("cell/name") for match in matches: print(match.text)This example works with a str. In this case, you have already the root. import xml.etree.ElementTree as ET xml_data = """<?xml version="1.0" encoding="utf-8" ?> <cli-output> <version>1.0</version> <timestamp>1646925662329</timestamp> <context cell="celadm06"/> <cell> <name>celadm06</name> <accessLevelPerm>remoteLoginEnabled</accessLevelPerm> <bbuStatus>normal</bbuStatus> <cpuCount>96/96</cpuCount> </cell> </cli-output>""" # here is no getroot() used doc = ET.fromstring(xml_data) match = doc.find("cell/name") if match is not None: print("Match found") print(match.text) else: print("Match was not found") RE: Get cell name from XML file - paulo79 - Mar-11-2022 (Mar-11-2022, 09:43 AM)DeaD_EyE Wrote: I copied your content to doc.xml. Thanks! So, I want to pass a value from an Oracle table column to that string. Like: xml_data=cursor.execute(select col1 from tab) Is there any example? How can I do that? Sorry, I’m on my mobile now. |