Home > Net >  Create an XML using etree
Create an XML using etree

Time:06-10

I am trying to create this :

<View>
  <Text name="text" value="$text" />
<View style="box-shadow: 2px 2px 5px #999;padding: 20px; margin-top: 2em;border-radius: 5px;display: flex; justify-content: space-between; background: #f5dcdc;">
  <Choices toName="text" name="label1">
    <Header value="label1"/>
    <Choice value="modality1"/>
    <Choice value="modality2"/>
    <Choice value="modality3"/>
  </Choices>
  <Choices toName="text" name="label2">
    <Header value="label2"/>
    <Choice value="modality1"/>
    <Choice value="modality2"/>
    <Choice value="modality3"/>
  </Choices>
  </View>
</View>

However when I start my code using:

    root = etree.Element("View")
    textelem = etree.SubElement(root, "Text")
    ###
    stylelem = etree.SubElement(root, "style")
    styledict = {"View":"box-shadow: 2px 2px 5px #999;padding: 20px; margin-top: 2em;border-radius: 5px;display: flex; justify-content: center; background: #f5dcdc;"}
    for key, val in styledict.items():
        stylelem.set(key, val)
    ###
    dictattrib = {
        "name": "text",
        "value": "$text"
    }
    for key, val in dictattrib.items():
        textelem.set(key, val)

I get this :

<View>
  <Text name="text" value="$text"/>
  <View style="box-shadow: 2px 2px 5px #999;padding: 20px; margin-top: 2em;border-radius: 5px;display: flex; justify-content: center; background: #f5dcdc;"/>
  <Header value="label1"/>
  <Choices toName="text" name="label1">
    <Choice value="modality1"/>
    <Choice value="modality2"/>
    <Choice value="modality3"/>
  </Choices>
  <Header value="label2"/>
  <Choices toName="text" name="label2">
    <Choice value="modality1"/>
    <Choice value="modality2"/>
  </Choices>
</View>

Anyone has any idea how to change the to get that not closing tag and get a closing tag in the end?

It should yield results here : https://labelstud.io/playground/

CodePudding user response:

The easy answear would be to append Choices to View instead of root. This will create a subtree of choice into View and it will open and close whenever you call it. Don't modify the xml using string, it has options.

  • Related