0

I am trying to replicate some XML code in python to put into a program I am currently working on which does some panoramic captures. At the end the idea is to export an XML file of the capture detail to allow for easier importing into one of the various panorama capture programs.

I am fairly new to Python, but have been using xml.etree.ElementTree, with this I can set information like the root declaration and the header and sub-header but I get a bit lost in two points, the first is how through a sub-element I can set a value (e.g. GPS) and the second is how a sub-element can have multiple values (e.g. mosaic / overlap minimium).

For the elements I had the below working;

root = etree.Element("papywizard")
root.set("version", "c")
header = etree.SubElement(root,"header")
general = etree.SubElement(header, "general")
title = etree.SubElement(general,"title")

I then thought i could do something like title.text("Test123") but this did not work. The full XML I am trying to replicate is below, is someone able to point me in the right direction on how I can set text within a sub-element tag, and beyond that how many tags can be aggregated into one sub-element?

Many Thanks!

<?xml version="1.0" encoding="utf-8"?>
<papywizard version="c">
    <header>
        <general>
            <title>
                Test Capture 1
            </title>
            <gps>
                37.8022697,-122.4056749
            </gps>
            <comment>
                Add your comments here
            </comment>
        </general>
        <shooting mode="mosaic">
            <headOrientation>
                up
            </headOrientation>
            <cameraOrientation>
                landscape
            </cameraOrientation>
            <stabilizationDelay>
               5.0
            </stabilizationDelay>
            <counter>
                001
            </counter>
            <startTime>
               2014-02-23_13h59m01s
            </startTime>
            <endTime>
               2014-02-23_13h53m33s
            </endTime>
       </shooting>
       <camera>
            <timeValue>
               5.0
            </timeValue>
            <bracketing nbPicts="1"/>
            <sensor coef ="4.74" ratio="4:3"/>
       </camera>
       <lens type="rectilinear">
            <focal>
               12.7
            </focal>
       </lens>
       <mosaic>
            <nbPicts pitch="5" yaw="10"/>
            <overlap minimum="0.25" pitch="0.25" yaw="0.25"/>
       </mosaic>
    </header>
    <shoot>
        <pict bracket="1" id="1">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="37.96" roll="0.0"  yaw="-99.96"/>
        </pict>
        <pict bracket="1" id="2">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="18.98" roll="0.0"  yaw="-99.96"/>
        </pict>
        <pict bracket="1" id="3">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="0.00" roll="0.0"  yaw="-99.96"/>
        </pict>
    </shoot>
</papywizard>

3 Answers 3

2
import xml.etree.ElementTree as ET

root = ET.Element("papywizard")
root.set("version", "c")
header = ET.SubElement(root,"header")
general = ET.SubElement(header, "general")
title = ET.SubElement(general,"title")
title.text = str('Test123') # This is how you set it

tree = ET.ElementTree(root) # This step will form a tree
tree.write('expected.xml') # This step will save the xml file.
Sign up to request clarification or add additional context in comments.

Comments

1

You must use the following command:

title.text = "some text"

Comments

1

Text nodes and element nodes are two kinds of nodes and an XML element node can have any number of text and/or element child nodes in any order.

If you want to add text to a node, you can do it with the .text attribute

title.text = "Sometext"

If you want to add attributes, you can do it with the set command

title.set('Attribute name', 'Attributevalue')

1 Comment

Thanks for all the help everyone, each answer worked perfectly :). Most appreciated!

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.