2

The image below is the result of performance testing. One of my issue is: How can I transfer my result into a XML file?

testdata

I've been trying file writing method. But what if the data I need to parse is massive? Is there a method or plugin to done it with a smarter way?

My code so far:

def generate_xml(jank_perc):

with open('jankyresult.xml','a') as f:
    f.write('<?xml version="1.0" ?>\n')
    f.write("<root>\n")
    f.write("   <doc>\n")
    f.write("      <Jankyframes>" + jank_perc + '%' + "</Jankyframes>\n")
    f.write("   </doc>\n")
    f.write("</root>\n")

I don't want to create manually. I hope there is a smarter way to do it. Thanks for answering!

1
  • You can use jinja2 Commented Aug 1, 2016 at 6:57

2 Answers 2

4

Use this ElementTree. An example of it is,

from xml.etree import ElementTree, cElementTree
from xml.dom import minidom

root = ElementTree.Element('root')
child1 = ElementTree.SubElement(root, 'doc')
child1_1 = ElementTree.SubElement(child1, 'Jankyframes')
child1_1.text = jank_perc

print ElementTree.tostring(root)
tree = cElementTree.ElementTree(root) # wrap it in an ElementTree instance, and save as XML

t = minidom.parseString(ElementTree.tostring(root)).toprettyxml() # Since ElementTree write() has no pretty printing support, used minidom to beautify the xml.
tree1 = ElementTree.ElementTree(ElementTree.fromstring(t))

tree1.write("filename.xml",encoding='utf-8', xml_declaration=True)
Sign up to request clarification or add additional context in comments.

3 Comments

"tree = cElementTree.ElementTree(root)" Can you maybe explain this code for me? Thanks, I really don't know why it's here.
The cElementTree module is a C implementation of the ElementTree API, optimized for fast parsing and low memory use. On typical documents, cElementTree is 15-20 times faster than the Python version of ElementTree, and uses 2-5 times less memory according to effbot.org/zone/celementtree.htm added a comment in front of that code as well.
Thanks for answering my question. I've notice the use of ElementTree.
1

Have a look at the yattag library:

Generate HTML or XML in a pythonic way. Pure python alternative to web template engines.Can fill HTML forms with default values and error messages.

Here's an example based on your code sample:

doc, tag, text = Doc().tagtext()

doc.asis('<?xml version="1.0"?>')

with tag('root'):
    with tag('doc'):
        with tag('Jankyframes'):
            text(jank_perc + '%')

with open('jankyresult.xml','a') as f:
    f.write(doc.getvalue())

Depending on how you get your input data (e.g. a list or dict), you could iterate over it and write your results to the XML while still keeping the code readable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.