Actually, my workflow is, I'll get some data from backend python, and I have to use that data and replicate it into an HTML page or in a pdf format as per the user's wish.
So I have created a python function in which the XML will be there and be saved automatically in our backend.
Here I'll provide my .py file where I wrote my code which generated XML code.
import xml.etree.ElementTree as xml
def GenerateXML(filename):
root = xml.Element("customers")
c1= xml.Element("customer")
root.append(c1)
type1= xml.SubElement(c1,"place")
type1.text = "UK"
Amount1 = xml.SubElement(c1,"Amount")
Amount1.text="4500"
tree=xml.ElementTree(root)
with open(filename,"wb") as f:
tree.write(f)
if __name__ == "__main__":
GenerateXML("fast.xml")
The result for this code will generate a backend file named fast.xml, which contains
#fast.xml
<customers>
<customer>
<place>uk</place>
<Amount>4500</Amount>
</customer>
</customers>
Creating an XML file is done, but attaching an XSL file to the XML is the issue,
can we do it with python, as we created .XML file
For example,
I have another XML file, which has an XSL file for it:
XML file
<?xml-stylesheet type = "text/xsl" href = "demo1.xsl"?>
<class>
<student>
<firstname>Graham</firstname>
<lastname>Bell</lastname>
<nickname>Garry</nickname>
</student>
<student>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
<nickname>Ally</nickname>
</student>
<student>
<firstname>Thomas</firstname>
<lastname>Edison</lastname>
<nickname>Eddy</nickname>
</student>
</class>
IN HTML it looks with a tabular form and with background colors.
but how to do it with an automated XML file
can anyone provide me a solution for this?
Thanks in Advance
Related
Need to extract Data from Power Designer XML File in python.
<?xml version="1.0" encoding="UTF-8"?>
<?PowerDesigner AppLocale="UTF16" ExtractDate=12345678"?>
<Model xmlns:a="attribute" xmlns:c="collection" x,;ms:o="objecct">
<o:RootObject Id="o1"
<a:SessionID>0000-0000</a:SessionID>
<c:Children>
<o:Model Id="o2"
<ObjectID>ABC-DEF</a:ObjectId>
<c:ExtendedCollections>
<c:Files>
<o:FileObject Id="012">
<a:ObjectID>123</a:ObjectID>
<a:Name>SAP_PD_PRC</a:Name>
<a:Code>SA{_PD_PRC_CD</a:Code>
</o:FileObject>
</c:Files>
</o:Model>
</o:RootObject>
</Model
the code i am using is:
import xml.etree.ElementTree as ET
tree = ET.parse('pd_eam.xml')
root = tree.getroot()
for node in tree.findall('.//o:RootObject Id/c:Children/o:Model Id/c:Files/*'):
print(node.tag,node.attrib,node.text)
Code:
This is working for other XML but not for Power Designer XML.
Need ObjectID,Name,Code from Files Tag.
Files can have multiple FileObject Id's
I'm trying to take an API call response and parse the XML data into list, but I am struggling with the multiple child/parent relationships.
My hope is to export a new XML file that would line up each job ID and tracking number, which I could then import into Excel.
Here is what I have so far
The source XML file looks like this:
<project>
<name>October 2019</name>
<jobs>
<job>
<id>5654206</id>
<tracking>
<mailPiece>
<barCode>00270200802095682022</barCode>
<address>Accounts Payable,1661 Knott Ave,La Mirada,CA,90638</address>
<status>En Route</status>
<dateTime>2019-10-12 00:04:21.0</dateTime>
<statusLocation>PONTIAC,MI</statusLocation>
</mailPiece>
</tracking>...
Code:
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement
tree = ET.parse('mailings.xml')
root = tree.getroot()
print(root.tag)
for x in root[1].findall('job'):
id=x.find('id').text
tracking=x.find('tracking').text
print(root[1].tag,id,tracking)
The script currently returns the following:
jobs 5654206 None
jobs 5654203 None
So I have the following .txt file of data, where the data highlighted with yellow needs to be saved to a new txt file:
I managed to print certain sections in Python, but that's about it:
with open('Podatki-zima-MEDVES.txt', mode='r+t') as file:
for line in file:
print(line[18:39])
Resulting in:
EntryDate="20101126"
EntryDate="20101126"
EntryDate="20101126"
EntryDate="20101126"
EntryDate="20101127"
EntryDate="20101128"
EntryDate="20101128"
EntryDate="20101128"
EntryDate="20101128"
I know it's a very basic question, but for someone experienced this wouldn't take a minute.
Thanks
It looks like you're trying to parse xml data.
There is a standard library package that can do this. The documentation is pretty good and it includes a tutorial. Take a look at The ElementTree XML API.
In you case the code would look something like:
data = """
<data>
<ROW EntryData="20101126" SnowDepth="4"/>
<ROW EntryData="20101127" SnowDepth="8"/>
</data>"""
import xml.etree.ElementTree as ET
root = ET.fromstring(data)
for child in root:
entries = child.attrib
print(entries["EntryData"], entries["SnowDepth"])
This gives the output you're looking for:
20101126 4
20101127 8
As an alternative to using Element Tree you could use an Expat parser for your Structured Markup data.
You first need to specify document type and wrap a top level element around your data as follows:
<?xml version="1.0"?>
<podatki>
<ROW RowState="5" EntryDate="20101126" Entry="" SnowDepth="4" />
<ROW RowState="13" EntryDate="20101126" Entry="Prvi sneg to zimo" SnowDepth="10" />
</podatki>
Then you could use an expat parser.
import xml.parsers.expat
def podatki(name, attrs):
if name == "ROW":
print(f'EntryDate={attrs["EntryDate"]},',
f'SnowDepth={attrs["SnowDepth"]}')
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = podatki
with open('podatki.xml', 'rb') as input_file:
parser.ParseFile(input_file)
The result should be
EntryDate=20101126, SnowDepth=4
EntryDate=20101126, SnowDepth=10
I have the following python method which runs though an xml file and parses it, and TRIES to edit a field:
import requests
import xml.etree.ElementTree as ET
import random
def runThrougheTree():
#open xml file
with open("testxml.xml") as xml:
from lxml import etree
#parse
parser = etree.XMLParser(strip_cdata=True, recover=True)
tree = etree.parse("testxml.xml", parser)
root= tree.getroot()
#ATTEMPT to edit field - will not work as of now
for ci in root.iter("CurrentlyInjured"):
ci.text = randomCurrentlyInjured(['sffdgdg', 'sdfsdfdsfsfsfsd','sfdsdfsdfds'])
#Overwrite initial xml file with new fields - will not work as of now
etree.ElementTree(root).write("testxml.xml",pretty_print=True, encoding='utf-8', xml_declaration=True)
#send post (Jboss)
requests.post('http://localhost:9000/something/RuleServiceImpl', data="testxml.xml)
def randomCurrentlyInjured(ran):
random.shuffle(ran)
return ran[0]
#-----------------------------------------------
if __name__ == "__main__":
runThrougheTree()
Edited XML file:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rule="http://somewebsite.com/" xmlns:ws="http://somewebsite.com/" xmlns:bus="http://somewebsite.com/">
<soapenv:Header/>
<soapenv:Body>
<ws:Respond>
<ws:busMessage>
<bus:SomeRef>insertnumericvaluehere</bus:SomeRef>
<bus:Content><![CDATA[<SomeDef>
<SomeType>ABCD</Sometype>
<A_Message>
<body>
<AnonymousField>
<RefIndicator>1111111111111</RefIndicator>
<OneMoreType>HIJK</OneMoreType>
<CurrentlyInjured>ABCDE</CurentlyInjured>
</AnonymousField>
</body>
</A_Message>
</SomeDef>]]></bus:Content>
<bus:MessageTypeId>somenumericvalue</bus:MessageTypeId>
</ws:busMessage>
</ws:Respond>
</soapenv:Body>
</soapenv:Envelope>
Issues:
The field is not being edited.
Jboss error: Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
Note: I have ensured that there is no characters prior to first xml tag.
In the end, I was unable to use lxml, elementtree to edit the fields/post to Jboss as:
I had CDATA in the xml as mzjn pointed out in the comments
Jboss did not like the request after it had been parsed, even when the CDATA tags were removed.
Workaround/Eventual SOlution: I was able to (somewhat tediously) use .replace() in my script to edit the plaintext successfully, and then send the POST via Jboss. I hope this helps someone else someday!
I am using Elementtree to parse an xml file, edit the contents and write to a new xml file. I have this all working apart form one issue. When I generate the file there are a lot of extra lines containing namespace information. Here are some snippets of code:
import xml.etree.ElementTree as ET
ET.register_namespace("", "http://clish.sourceforge.net/XMLSchema")
tree = ET.parse('ethernet.xml')
root = tree.getroot()
commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')
for command in commands:
all1.append(list(command.iter()))
And a sample of the output file, with the erroneous line xmlns="http://clish.sourceforge.net/XMLSchema:
<COMMAND xmlns="http://clish.sourceforge.net/XMLSchema" help="Interface specific description" name="description">
<PARAM help="Description (must be in double-quotes)" name="description" ptype="LINE" />
<CONFIG />
</COMMAND>
How can I remove this with elementtree, can I? Or will i have to use some regex (I am writing a string to the file)?