Can I create this XML file using lxml?

I'm trying to create an xml that looks something 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 is annotated part of the VFX script node workflow and this file is part of the "media read" node. I've been looking at different things all week but couldn't find anything close to this. I chose lxml for nice printing. I've managed to create a bunch of other simpler (for me) xml files, but for that I have to say ... I'm lost. Complete so far! Can someone kindly shed some light on this please?

MY QUESTIONS: - is lxml suitable for this? - if not, which choice is better? (I was actually looking for an ElementTree example, no luck!) - if so, where to start? Can anyone share a piece of code to get me started?

What I could create so far was 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 these are "acceptable" questions. Thanks in advance for any help.

+3


source to share


1 answer


First create a format

node and then add it to the root of XML

node.

Sample 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))

      



Printing

<?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>

      

+1


source







All Articles