3

I'm trying to generate an xml that looks exactly like this:

<?xml version="1.0" encoding="utf-8"?>
<XML type="formats" version="4">
  <format type="format" uid="BEAUTY:MasterBeauty">
    <type>video</type>
    <channelsDepth type="uint">16</channelsDepth>
    <channelsEncoding type="string">Float</channelsEncoding>
    <channelsEndianess type="string">Little Endian</channelsEndianess>
    <fieldDominance type="int">2</fieldDominance>
    <height type="uint">1080</height>
    <nbChannels type="uint">4</nbChannels>
    <pixelLayout type="string">ABGR</pixelLayout>
    <pixelRatio type="float">1</pixelRatio>
    <rowOrdering type="string">up</rowOrdering>
    <width type="uint">1920</width>
  </format>
</XML>

It's part of a VFX nodal workflow script ensemble and this file is part of a "read media" node. I've spent the whole week looking at many different things but can't find anything close to this. I picked lxml for the pretty print thing. I was able to generate a bunch of other simpler (to me) xml files but for this one, I gotta say … i'm lost. Complete fail so far! Could someone kindly shed a light on this please?

MY QUESTIONS: - is lxml appropriate for this? - if no, what is a better choice? (i did look for ElementTree example, no luck!) - if yes, where do i start? Could someone share a piece a code to get me started?

What i could create so far was things like this one:

import os, sys
import lxml.etree
import lxml.builder as lb
from lxml import etree 

E = lxml.builder.ElementMaker()
Setup = E.Setup
Base = E.Base
Version = E.Version
Note = E.Note
Expanded = E.Expanded
ScrollBar = E.ScrollBar
Frames = E.Frames
Current_Time = E.Current_Time
Input_DataType = E.Input_DataType
ClampMode = E.ClampMode
AdapDegrad = E.AdapDegrad
UsedAsTransition = E.UsedAsTransition
State = E.State


root_node = Setup(
        Base(
            Version('12.030000'),
            Note(''),
            Expanded('False'),
            ScrollBar('0'),
            Frames('0'),
            Current_Time('1'),
            Input_DataType('3'),
            ClampMode('0'),
            AdapDegrad('False'),
            UsedAsTransition('False')
            ),
        State(),
                  )
print lxml.etree.tostring(root_node, pretty_print=True)

str = etree.tostring(root_node, pretty_print=True)

myXMLfile = open('/Users/stefan/XenDRIVE/___DEV/PYTHON/Create_xlm/create_Batch_xml_setups/result/xml_result/root.root_node.xml', 'w')
myXMLfile.write(str)
myXMLfile.close()

Hope those are "acceptable" questions. Thank you in advance for any help.

1 Answer 1

2

First, make the format node and then add it to the root XML node.

Example code (follow it to create more nodes):

from lxml import etree
from lxml.builder import ElementMaker

E = ElementMaker()

format = E.format(
    E.type("video"),
    E.channelsDepth("16", type="uint"),
    # create more elements here        

    type="format",
    uid="BEAUTY:MasterBeauty"
)
root = E.XML(
    format,

    type="formats",
    version="4"
)

print(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))

Prints:

<?xml version='1.0' encoding='utf-8'?>
<XML version="4" type="formats">
  <format type="format" uid="BEAUTY:MasterBeauty">
    <type>video</type>
    <channelsDepth type="uint">16</channelsDepth>
  </format>
</XML>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh Yes! This is precisely what i was asking for! A few minutes, 1 answer and boom, right on … vs 1 week of getting lost! Thank you so much @alecxe!!! right now, i'm joyfully crying and repeating your name again and again :)

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.