Python Forum

Full Version: Get cell name from XML file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I have an oracle table with CLOB column type with this content below:

Quote:<?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>

How can I create a python program to get <cell> <name> from that column in oracle table ?
You already know how to parse XML data, as this thread shows. Just do this with the content extracted from the database.
(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
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")
(Mar-11-2022, 09:43 AM)DeaD_EyE Wrote: [ -> ]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")



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.