Building a generic XML parser in Python? - python

I am a newbie and having 1 week experience writing python scripts.
I am trying to write a generic parser (Library for all my future jobs) which parses any input XML without any prior knowledge of tags.
Parse input XML.
Get the values from the XML and Set the values basing on the tags.
Use these values in the rest of the job.
I am using the "xml.etree.ElementTree" library and i am able to parse the XML in the below mentioned way.
#!/usr/bin/python
import os
import xml.etree.ElementTree as etree
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info('start reading XML property file')
filename = "mood_ib_history_parameters_DEV.xml"
logger.info('getting the current location')
__currentlocation__ = os.getcwd()
__fullpath__ = os.path.join(__currentlocation__,filename)
logger.info('start parsing the XML property file')
tree = etree.parse(__fullpath__)
root = tree.getroot()
hive_db = root.find("hive_db").text
EDGE_HIVE_CONN = root.find("EDGE_HIVE_CONN").text
target_dir = root.find("target_dir").text
to_email_alias = root.find("to_email_alias").text
to_email_cc = root.find("to_email_cc").text
from_email_alias = root.find("from_email_alias").text
dburl = root.find("dburl").text
SQOOP_EDGE_CONN = root.find("SQOOP_EDGE_CONN").text
user_name = root.find("user_name").text
password = root.find("password").text
IB_log_table = root.find("IB_log_table").text
SR_DG_master_table = root.find("SR_DG_master_table").text
SR_DG_table = root.find("SR_DG_table").text
logger.info('Hive DB %s', hive_db)
logger.info('Hive DB %s', hive_db)
logger.info('Edge Hive Connection %s', EDGE_HIVE_CONN)
logger.info('Target Directory %s', target_dir)
logger.info('To Email address %s', to_email_alias)
logger.info('CC Email address %s', to_email_cc)
logger.info('From Email address %s', from_email_alias)
logger.info('DB URL %s',dburl)
logger.info('Sqoop Edge node connection %s',SQOOP_EDGE_CONN)
logger.info('Log table name %s',IB_log_table)
logger.info('Master table name %s',SR_DG_master_table)
logger.info('Data governance table name %s',SR_DG_table)
Now the question is if i want to parse an XML without any knowledge of the tags and elements and use the values how do i do it. I have gone through multiple tutorials but all of them help me with parsing the XML by using the tags like below
SQOOP_EDGE_CONN = root.find("SQOOP_EDGE_CONN").text
Can anybody point me to a right tutorial or library or a code snippet to parse the XML dynamically.

I think official documentation is pretty clear and contains some examples: https://docs.python.org/3/library/xml.etree.elementtree.html
The main part you need to implement is loop over the child nodes (potentially recursively):
for child in root:
# child.tag contains the tag name, child.attrib contains the attributes
print(child.tag, child.attrib)

Well parsing is easy as that - etree.parse(path)
Once you've got the root in hand using tree.getroot() you can just iterate over the tree using Python's "in":
for child_node in tree.getroot():
print child_node.text
Then, to see tags these child_nodes have, you do the same trick.
This lets you go over all tags in the XML without having to know the tag names at all.

Related

Getting values from XML Url Python

I have an account in https://es.besoccer.com/ and they have an api for getting data in a xml.
I have this code in python for print the values of the xml I need:
from xml.dom import minidom
doc = minidom.parse("datos.xml")
partidos = doc.getElementsByTagName("matches")
for partido in partidos:
local = partido.getElementsByTagName("local")[0]
visitante = partido.getElementsByTagName("visitor")[0]
print("local:%s" % local.firstChild.data)
print("visitante:%s" % visitante.firstChild.data)
canales=partido.getElementsByTagName("channels")
for canal in canales:
nombre=canal.getElementsByTagName("name")[0]
print("canal:%s" % nombre.firstChild.data)
The problem is thatthe XML of this site is a url so I donĀ“t know how to read the xml directly form the url. Other problem is that the xml contains some tags that are a link, and python throughs a error with that tags that contains a url.
Read the API docs here: https://www.besoccer.com/api/documentacion
After you understand which API call you need to use, prepare the URL and the query arguments and use a library like requests in order to read the data.
Once you have the reply (assuming it is XML based) - you can use your code and parse it.

Parsing XML files and validating against xsd schema

This is an example of XML output I need to parse and validate against the schema xsd files.
<Record_Delimiter DocumentID="1.1" DocumentType="PARENT" DocumentName="SCHOOL" RelatedDocumentID=""/>
<xs:SCHOOL>
<xs:Name>some name</xs:Name>
<xs:ID>5908390481</xs:ID>
<xs:Address>some address</xs:Address>
</xs:SCHOOL>
<Record_Delimiter DocumentID="1.2" DocumentType="CHILD" DocumentName="STUDENTEXP" RelatedDocumentID="1.1"/>
<xs:STUDENTEXP>
<xs:STUDENT>
<xs:Name>some name</xs:Name>
<xs:SID>s1036456</xs:SID>
<xs:Age>12</xs:Age>
<xs:Address>some address</xs:Address>
<xs:Expenses>
<xs:Fees>800</xs:Fees>
<xs:Books>100</xs:Books>
<xs:Uniform>50</xs:Uniform>
<xs:Transport>10</xs:Transport>
</xs:Expenses>
</xs:STUDENT>
</xs:STUDENTEXP>
<Record_Delimiter DocumentID="1.3" DocumentType="CHILD" DocumentName="STUDENTEXP" RelatedDocumentID="1.1"/>
<xs:STUDENTEXP>
<xs:STUDENT>
<xs:Name>some name</xs:Name>
<xs:SID>s1036789</xs:SID>
<xs:Age>15</xs:Age>
<xs:Address>some address</xs:Address>
<xs:Expenses>
<xs:Fees>1000</xs:Fees>
<xs:Books>200</xs:Books>
<xs:Uniform>50</xs:Uniform>
<xs:Transport>10</xs:Transport>
</xs:Expenses>
</xs:STUDENT>
</xs:STUDENTEXP>
This file itself is not valid XML because there is no single tag wrapping all the other tags. But each record (ie, SCHOOL and STUDENTEXP)is valid XML and it validates against the schema (school.xsd, studentexp.xsd).
I never worked with this format and not sure about few things, like how to parse such a file programmatically?
Normally using lxml, we can validate each record if it was in a separate file:
xmlschema = etree.XMLSchema(etree.parse('./studentexp.xsd'))
xmlschema.assertValidate(etree.parse('./sampleStudentexp.xml'))
What is the proper way to extract the "records" and validate them separately?
This question has been asked before: Parse a xml file with multiple root element in python
I suspect there there is a single-pass solution that involves using a stream parser. My Python isn't strong enough to work out whether it's possible. Anyway - one of the solutions in that thread might be good enough.
lxml has event parsing based on tags.
incremental-event-parsing
and the below worked.
parser = etree.XMLPullParser(events=('start', 'end'))
events = parser.read_events()
with open('.\sample.xml', 'rb') as f:
d1 = deque()
for line in f:
parser.feed(line)
for action, e in events:
if action == 'start':
d1.append(e.tag)
elif action == 'end' and len(d1) == 1:
if d1.pop() == e.tag:
root = parser.close()
print(etree.tostring(root, pretty_print=True, encoding="UTF-8").decode("UTF-8"))
else:
d1.pop()

Parsing HTML with entity ref

I am trying to parse some HTML which has as an example
<solids>
&sub2;
</solids>
The html file is read in as a string. I need to insert the HTML from a file that sub2 defines into the appropriate part of the string before then processing the whole string as XML.
I have tried HTMLParser and using its handlers with
class MyHTMLParser(HTMLParser):
def handle_entityref(self, name):
# This gets called when the entity is referenced
print "Entity reference : "+ name
print "Current Section : "+ self.get_starttag_text()
print self.getpos()
But getpos returns a line number and offset rather than position in the string. ( The insertion can be at any point in the file )
I found this link and this suggest to use lxml. I have looked at lxml but cannot see how it would solve the problem. Its scanner does not seem to have an entity handler and seems to be xml rather than html
Okay found that lxml will handle the ENTITY references for me.
Just had to setup parser with the option resolve_entities=True
parser = etree.XMLParser(resolve_entities=True)
root = etree.parse(filename, parser=parser)

How to create Jenkins Job with Customized XML in python

I am trying to create a Jenkins job using Jenkins module in python. I am able to successfully connect with jenkins and perfrom get job_count as well as create_job() method.
In create_job() method i can perfrom this operation only with "jenkins.EMPTY_CONFIG_XML" parameter. How do i pass my own xml config file? below is my code, I have config saved on local, how to pass it by replacing EMPTY_CONFIG_XML. I tried few things, didn't work. Couldn't find it online. My below code is working. It's creating TestJob with EMPTY_CONFIG_XML. can someone please help how to pass customized XML file? Thank you for help!
import jenkins
import xml.etree.ElementTree as ET
server = jenkins.Jenkins("http://x.x.x.x:8080", username="foo", password="baar")
#print server.get_whoami()
server.create_job("TestJob",jenkins.EMPTY_CONFIG_XML)
Looking through the documentation for create_job, the config_xml parameter should be passed as a string representation of the xml.
I used a xml.etree.ElementTree to parse the XML file and convert it into a string:
import xml.etree.ElementTree as ET
def convert_xml_file_to_str():
tree = ET.parse(path_to_config_file)
root = tree.getroot()
return ET.tostring(root, encoding='utf8', method='xml').decode()
def main():
target_server = jenkins.Jenkins(url, username=username, password=password)
config = convert_xml_file_to_str()
target_server.create_job(job_name, config)
main()
I found this thread very helpful in understanding how to parse XML files, it also has a nice explanation about differences between Python2/3.

Python 3.4 - XML Parse - IndexError: List Index Out of Range - How do I find range of XML?

Okay guys, I'm new to parsing XML and Python, and am trying to get this to work. If someone could help me with this it would be greatly appreciated. If you can help me (educate me) on how to figure it out for myself, that would be even better!
I am having trouble trying to figure out the range to reference for an XML document as I can't find any documentation on it. Here is my code and I'll include the entire Traceback after.
#import library to do http requests:
import urllib.request
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
#download the file:
file = urllib.request.urlopen('http://www.wizards.com/dndinsider/compendium/CompendiumSearch.asmx/KeywordSearch?Keywords=healing%20%word&nameOnly=True&tab=')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('Data.Results.Power.ID')[0].toxml()
#strip off the tag (<tag>data</tag> ---> data):
xmlData=xmlTag.replace('<id>','').replace('</id>','')
#print out the xml tag and data in this format: <tag>data</tag>
print(xmlTag)
#just print the data
print(xmlData)
Traceback
/usr/bin/python3.4 /home/mint/PycharmProjects/DnD_Project/Power_Name.py
Traceback (most recent call last):
File "/home/mint/PycharmProjects/DnD_Project/Power_Name.py", line 14, in <module>
xmlTag = dom.getElementsByTagName('id')[0].toxml()
IndexError: list index out of range
Process finished with exit code 1
print len( dom.getElementsByTagName('id') )
EDIT:
ids = dom.getElementsByTagName('id')
if len( ids ) > 0 :
xmlTag = ids[0].toxml()
# rest of code
EDIT: I add example because I saw in other comment tha you don't know how to use it
BTW: I add some comment in code about file/connection
import urllib.request
from xml.dom.minidom import parseString
# create connection to data/file on server
connection = urllib.request.urlopen('http://www.wizards.com/dndinsider/compendium/CompendiumSearch.asmx/KeywordSearch?Keywords=healing%20%word&nameOnly=True&tab=')
# read from server as string (not "convert" to string):
data = connection.read()
#close connection because we dont need it anymore:
connection.close()
dom = parseString(data)
# get tags from dom
ids = dom.getElementsByTagName('Data.Results.Power.ID')
# check if there are any data
if len( ids ) > 0 :
xmlTag = ids[0].toxml()
xmlData=xmlTag.replace('<id>','').replace('</id>','')
print(xmlTag)
print(xmlData)
else:
print("Sorry, there was no data")
or you can use for loop if there is more tags
dom = parseString(data)
# get tags from dom
ids = dom.getElementsByTagName('Data.Results.Power.ID')
# get all tags - one by one
for one_tag in ids:
xmlTag = one_tag.toxml()
xmlData = xmlTag.replace('<id>','').replace('</id>','')
print(xmlTag)
print(xmlData)
BTW:
getElementsByTagName() expects tagname ID - not path Data.Results.Power.ID
tagname is ID so you have to replace <ID> not <id>
for this tag you can event use one_tag.firstChild.nodeValue in place of xmlTag.replace
.
dom = parseString(data)
# get tags from dom
ids = dom.getElementsByTagName('ID') # tagname
# get all tags - one by one
for one_tag in ids:
xmlTag = one_tag.toxml()
#xmlData = xmlTag.replace('<ID>','').replace('</ID>','')
xmlData = one_tag.firstChild.nodeValue
print(xmlTag)
print(xmlData)
I haven't used the built in xml library in a while, but it's covered in Mark Pilgrim's great Dive into Python book.
-- I see as I'm typing this that your question has already been answered but since you mention being new to Python I think you will find the text useful for xml parsing and as an excellent introduction to the language.
If you would like to try another approach to parsing xml and html, I highly recommend lxml.

Categories