I have tried to save the xml file in the following variable and later work on it as normal xml file. This is not working. How can I approach this situation. I need to edit the xml file without editing in the original file and without creating a new xml file. Is that possible?
comment_2 = open("cool.xml").read()
Thanks and Regards
You can use xml.etree.ElementTree to parse the XML file and then save it to a variable:
import xml.getElementTree as ET
tree = ET.parse('xml.etr.xml')
root = tree.getroot()
root.save(root)
root_variable = root_variable
Then you can save the xml file to an instance of ElementTree.
Related
I have an XML file that is when parsed reads a root different from the root I want it to read from.
the file starts with a tree that shows a summary of the data included in the file then another tree with the actual data in the subsequent section. However, when I parse it. it doesn't even read the tree that includes the data. It only reads the tree that includes the summary.
My question is how to modify my code to start reading the file from the tree that includes the data that I need
The XML file starts like that
I tried parsing it using xml.etree.ElementTree it reads the root "WorkflowSearch" what I want is for it to read the root at "SearchResults" so that I can read the fields into a pandas DataFrame
`tree = ET.parse('Workflows.xml')`
`root = tree.getroot()`
`root.tag, root.attrib`
I appreciate your help
I have a directory of xml files and I'm trying to merge them all into one big xml file
full = ET.Element('dataset')
for filename in glob.glob(os.path.join(path, '*.xml')):
tree = ET.parse(filename, parser=xmlp)
root = tree.getroot()
for pair in root: #root.iter('pair'):
full.append(pair)
I tried the above code and get this trivial error:
ParseError: parsing finished: line 330, column 0
The problem is that only the first file is appended to the new xml doc, how can I avoid this? Or is there a better way of merging? (The structures are identical)
Edit: they are of this structure:
<dataset>
<pair>
<t1></t1>
<t2></t2>
</pair>
...
</dataset>
Update: Used XML Copy Editor, and couldn't open told me unknown encoding MS932 even though it is in ISO-8859-1. Same error I got from trying to open with lxml and not xml in python. Manually recreated a new xml, not really a solution but oh well.
Thanks
I am in a project in which I have the task to convert a text file to xml file.
But the restriction is, the file names should be same. I use element tree
module of python for this but while writing to the tree I use tree.write()
method in which I have to explicitly define / hardcode the xml filename
myself.
I want to know is there any procedure to automatically create the .xml file
with same name as the text file
the sh module provides great flexibility and may help you for what you want to do if I understand the question correctly. I have shown an example in the following:
import sh
name = "filename.xml"
new_name = name[-3] + "txt"
sh.touch(new_name)
from there you can open the created file and write directly to it.
How can I create KML files from the XML files using python.
I have a lot of XML files. I have already parsed the data from the XML files using the SAX parser.
Now I want to create KML files from the data that I have parsed.
Is there any other way apart from xml.dom.minidom to write the KML file.
I am currently thinking of creating a template KML file. Then copying the template KML file and filling the 'data' in it.
Can anybody suggest a better way ?
My main concern is Maintainability (writing the data using minidom is pretty confusing for somebody to read).
Try xml.etree.ElementTree. Here's a short example creating a couple of points in a KML file:
from xml.etree import ElementTree as et
class Kml(object):
def __init__(self):
self.root = et.Element('kml')
self.doc = et.SubElement(self.root,'Document')
def add_placemark(self,name,desc,lat,long,alt):
pm = et.SubElement(self.doc,'Placemark')
et.SubElement(pm,'name').text = name
et.SubElement(pm,'description').text = desc
pt = et.SubElement(pm,'Point')
et.SubElement(pt,'coordinates').text = '{},{},{}'.format(lat,long,alt)
def write(self,filename):
tree = et.ElementTree(self.root)
tree.write(filename)
kml = Kml()
kml.add_placemark('Location1','Description1',-120,45,0)
kml.add_placemark('Location2','Description2',60,-45,0)
kml.write('out.kml')
I have this xml model.
link text
So I have to add some node (see the text commented) to this file.
How I can do it?
I have writed this partial code but it doesn't work:
xmldoc=minidom.parse(directory)
child = xmldoc.createElement("map")
for node in xmldoc.getElementsByTagName("Environment"):
node.appendChild(child)
Thanks in advance.
I downloaded your sample xml file and your code works fine. Your problem is most likely with the line: xmldoc=minidom.parse(directory), should this not be the path to the file you are trying to parse not to a directory? The parse() function parses an XML file it does not automatically parse all the XML files in a given directory.
If you change your code to something like below this should work fine:
xmldoc=minidom.parse("directory/model_template.xml")
child = xmldoc.createElement("map")
for node in xmldoc.getElementsByTagName("Environment"):
node.appendChild(child)
If you then execute the statement: print xmldoc.toxml() you will see that the map element has indeed been added to the Environment element: <Environment><map/></Environment>.