An lxml library that does not add new lines or indents to the tree after adding a new element

The name is self-evident, and before marking it as a duplicate, please think that I checked this answer and it does not work for me, because I do not even get the correct format in sys.stdout, not only when writing to a file. So I have the following xml (test.xml):

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www...">
  <soap:Body>
    <SubmitTransaction xmlns="http://www.">
      <Authentication>
      </Authentication>
      <Transaction>
        <DataFields>
        </DataFields>
      </Transaction>
    </SubmitTransaction>
  </soap:Body>
</soap:Envelope>

And the following code:

from lxml import etree

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("test.xml", parser)

def get_data_fields():
    for node in tree.iter():
        if 'DataFields' in node.tag:
            return node
a = get_data_fields()
field = etree.Element('Field_1')
child_1 = etree.Element('FieldName')
child_2 = etree.Element('FieldValue')
child_3 = etree.Element('FieldIndex')
child_1.text = 'dateTime'
child_2.text = '2016-07-29T12:00:00'
child_3.text = '1'

for i in [child_1, child_2, child_3]:
    field.append(i)
a.append(field)

s = etree.tostring(tree, pretty_print=True)
print(s.decode('utf-8'))

OUTPUT

<soap:Envelope xmlns:soap="http://www...">
  <soap:Body>
    <SubmitTransaction xmlns="http://www.">
      <Authentication>
      </Authentication>
      <Transaction>
        <DataFields>
        <Field_1><FieldName>dateTime</FieldName><FieldValue>2016-07-29T12:00:00</FieldValue><FieldIndex>1</FieldIndex></Field_1></DataFields>
      </Transaction>
    </SubmitTransaction>
  </soap:Body>
</soap:Envelope>

EXPECTED

<soap:Envelope xmlns:soap="http://www...">
  <soap:Body>
    <SubmitTransaction xmlns="http://www.">
      <Authentication>
      </Authentication>
      <Transaction>
        <DataFields>
          <Field_1>
            <FieldName>dateTime</FieldName>
            <FieldValue>2016-07-29T12:00:00</FieldValue>
            <FieldIndex>1</FieldIndex>
          </Field_1>
        </DataFields>
      </Transaction>
    </SubmitTransaction>
  </soap:Body>
</soap:Envelope>

I really don’t understand why the new field that I am adding is not formatted as intended, because if I print only field, everything looks fine:

s = etree.tostring(root, pretty_print=True)
print(s.decode('utf-8'))

#<Field_1 xmlns="http://www." xmlns:soap="http://www...">
#  <FieldName>dateTime</FieldName>
#  <FieldValue>2016-07-29T12:00:00</FieldValue>
#  <FieldIndex>1</FieldIndex>
#</Field_1>

NOTE. I am using python 3.4 (which is why I should .decode('utf-8'), otherwise I just get byte literals).

+4
source share
1

, a = get_data_fields():

a.text = None

lxml , , .

. http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output:

, XML- ( , ), DTD, , , , , None:

+2

All Articles