Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get cell name from XML file
#1
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 ?
Reply
#2
You already know how to parse XML data, as this thread shows. Just do this with the content extracted from the database.
Reply
#3
(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
Reply
#4
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")
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Only first letter in the cell of the file. rturus 7 3,203 Jun-27-2019, 02:46 PM
Last Post: perfringo
  Python not reading cell in excel file wendysling 1 2,160 May-21-2019, 10:40 PM
Last Post: Larz60+
  Problem with assigning directory of a file based on cell name mmaz67 3 2,841 Jul-04-2018, 08:40 AM
Last Post: Larz60+
  find cell value with matching regular expression of a row in excel file hruday 4 30,951 Jul-05-2017, 01:02 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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