lxml (etree) - Pretty Print attributes of root tag












1















Is it possible in python to pretty print the root's attributes?



I used etree to extend the attributes of the child tag and then I had overwritten the existing file with the new content. However during the first generation of the XML, we were using a template where the attributes of the root tag were listed one per line and now with the etree I don't manage to achieve the same result.



I found similar questions but they were all referring to the tutorial of etree, which I find incomplete.



Hopefully someone has found a solution for this using etree.



EDIT: This is for custom XML so HTML Tidy (which was proposed in the comments), doesn't work for this.



Thanks!



generated_descriptors = list_generated_files(generated_descriptors_folder)
counter = 0
for g in generated_descriptors:
if counter % 20 == 0:
print "Extending Descriptor # %s out of %s" % (counter, len(descriptor_attributes))

with open(generated_descriptors_folder + "\" + g, 'r+b') as descriptor:
root = etree.XML(descriptor.read(), parser=parser)

# Go through every ContextObject to check if the block is mandatory
for context_object in root.findall('ContextObject'):
for attribs in descriptor_attributes:
if attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'false')
elif attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] not in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'true')

# Sort the ContextObjects based on allow-null and their name
context_objects = root.findall('ContextObject')
context_objects_sorted = sorted(context_objects, key=lambda c: (c.attrib['allow-null'], c.attrib['name']))

root[:] = context_objects_sorted

# Remove mandatoryobjects from Descriptor attributes and pretty print
root.attrib.pop("mandatoryobjects", None)
# paste new line here


# Convert to string in order to write the enhanced descriptor
xml = etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True)

# Write the enhanced descriptor
descriptor.seek(0) # Set cursor at beginning of the file
descriptor.truncate(0) # Make sure that file is empty
descriptor.write(xml)

descriptor.close()

counter+=1









share|improve this question

























  • I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

    – Arne Uten
    Nov 14 '18 at 15:44













  • Perhaps this helps: stackoverflow.com/q/40410923/407651

    – mzjn
    Nov 14 '18 at 15:56











  • Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

    – Arne Uten
    Nov 14 '18 at 16:14













  • It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

    – mzjn
    Nov 14 '18 at 16:34











  • You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

    – mzjn
    Nov 14 '18 at 16:42
















1















Is it possible in python to pretty print the root's attributes?



I used etree to extend the attributes of the child tag and then I had overwritten the existing file with the new content. However during the first generation of the XML, we were using a template where the attributes of the root tag were listed one per line and now with the etree I don't manage to achieve the same result.



I found similar questions but they were all referring to the tutorial of etree, which I find incomplete.



Hopefully someone has found a solution for this using etree.



EDIT: This is for custom XML so HTML Tidy (which was proposed in the comments), doesn't work for this.



Thanks!



generated_descriptors = list_generated_files(generated_descriptors_folder)
counter = 0
for g in generated_descriptors:
if counter % 20 == 0:
print "Extending Descriptor # %s out of %s" % (counter, len(descriptor_attributes))

with open(generated_descriptors_folder + "\" + g, 'r+b') as descriptor:
root = etree.XML(descriptor.read(), parser=parser)

# Go through every ContextObject to check if the block is mandatory
for context_object in root.findall('ContextObject'):
for attribs in descriptor_attributes:
if attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'false')
elif attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] not in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'true')

# Sort the ContextObjects based on allow-null and their name
context_objects = root.findall('ContextObject')
context_objects_sorted = sorted(context_objects, key=lambda c: (c.attrib['allow-null'], c.attrib['name']))

root[:] = context_objects_sorted

# Remove mandatoryobjects from Descriptor attributes and pretty print
root.attrib.pop("mandatoryobjects", None)
# paste new line here


# Convert to string in order to write the enhanced descriptor
xml = etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True)

# Write the enhanced descriptor
descriptor.seek(0) # Set cursor at beginning of the file
descriptor.truncate(0) # Make sure that file is empty
descriptor.write(xml)

descriptor.close()

counter+=1









share|improve this question

























  • I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

    – Arne Uten
    Nov 14 '18 at 15:44













  • Perhaps this helps: stackoverflow.com/q/40410923/407651

    – mzjn
    Nov 14 '18 at 15:56











  • Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

    – Arne Uten
    Nov 14 '18 at 16:14













  • It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

    – mzjn
    Nov 14 '18 at 16:34











  • You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

    – mzjn
    Nov 14 '18 at 16:42














1












1








1








Is it possible in python to pretty print the root's attributes?



I used etree to extend the attributes of the child tag and then I had overwritten the existing file with the new content. However during the first generation of the XML, we were using a template where the attributes of the root tag were listed one per line and now with the etree I don't manage to achieve the same result.



I found similar questions but they were all referring to the tutorial of etree, which I find incomplete.



Hopefully someone has found a solution for this using etree.



EDIT: This is for custom XML so HTML Tidy (which was proposed in the comments), doesn't work for this.



Thanks!



generated_descriptors = list_generated_files(generated_descriptors_folder)
counter = 0
for g in generated_descriptors:
if counter % 20 == 0:
print "Extending Descriptor # %s out of %s" % (counter, len(descriptor_attributes))

with open(generated_descriptors_folder + "\" + g, 'r+b') as descriptor:
root = etree.XML(descriptor.read(), parser=parser)

# Go through every ContextObject to check if the block is mandatory
for context_object in root.findall('ContextObject'):
for attribs in descriptor_attributes:
if attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'false')
elif attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] not in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'true')

# Sort the ContextObjects based on allow-null and their name
context_objects = root.findall('ContextObject')
context_objects_sorted = sorted(context_objects, key=lambda c: (c.attrib['allow-null'], c.attrib['name']))

root[:] = context_objects_sorted

# Remove mandatoryobjects from Descriptor attributes and pretty print
root.attrib.pop("mandatoryobjects", None)
# paste new line here


# Convert to string in order to write the enhanced descriptor
xml = etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True)

# Write the enhanced descriptor
descriptor.seek(0) # Set cursor at beginning of the file
descriptor.truncate(0) # Make sure that file is empty
descriptor.write(xml)

descriptor.close()

counter+=1









share|improve this question
















Is it possible in python to pretty print the root's attributes?



I used etree to extend the attributes of the child tag and then I had overwritten the existing file with the new content. However during the first generation of the XML, we were using a template where the attributes of the root tag were listed one per line and now with the etree I don't manage to achieve the same result.



I found similar questions but they were all referring to the tutorial of etree, which I find incomplete.



Hopefully someone has found a solution for this using etree.



EDIT: This is for custom XML so HTML Tidy (which was proposed in the comments), doesn't work for this.



Thanks!



generated_descriptors = list_generated_files(generated_descriptors_folder)
counter = 0
for g in generated_descriptors:
if counter % 20 == 0:
print "Extending Descriptor # %s out of %s" % (counter, len(descriptor_attributes))

with open(generated_descriptors_folder + "\" + g, 'r+b') as descriptor:
root = etree.XML(descriptor.read(), parser=parser)

# Go through every ContextObject to check if the block is mandatory
for context_object in root.findall('ContextObject'):
for attribs in descriptor_attributes:
if attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'false')
elif attribs['descriptor_name'] == g[:-11] and context_object.attrib['name'] not in attribs['attributes']['mandatoryobjects']:
context_object.set('allow-null', 'true')

# Sort the ContextObjects based on allow-null and their name
context_objects = root.findall('ContextObject')
context_objects_sorted = sorted(context_objects, key=lambda c: (c.attrib['allow-null'], c.attrib['name']))

root[:] = context_objects_sorted

# Remove mandatoryobjects from Descriptor attributes and pretty print
root.attrib.pop("mandatoryobjects", None)
# paste new line here


# Convert to string in order to write the enhanced descriptor
xml = etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True)

# Write the enhanced descriptor
descriptor.seek(0) # Set cursor at beginning of the file
descriptor.truncate(0) # Make sure that file is empty
descriptor.write(xml)

descriptor.close()

counter+=1






python lxml pretty-print






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 10:46







Arne Uten

















asked Nov 14 '18 at 15:33









Arne UtenArne Uten

64




64













  • I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

    – Arne Uten
    Nov 14 '18 at 15:44













  • Perhaps this helps: stackoverflow.com/q/40410923/407651

    – mzjn
    Nov 14 '18 at 15:56











  • Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

    – Arne Uten
    Nov 14 '18 at 16:14













  • It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

    – mzjn
    Nov 14 '18 at 16:34











  • You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

    – mzjn
    Nov 14 '18 at 16:42



















  • I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

    – Arne Uten
    Nov 14 '18 at 15:44













  • Perhaps this helps: stackoverflow.com/q/40410923/407651

    – mzjn
    Nov 14 '18 at 15:56











  • Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

    – Arne Uten
    Nov 14 '18 at 16:14













  • It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

    – mzjn
    Nov 14 '18 at 16:34











  • You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

    – mzjn
    Nov 14 '18 at 16:42

















I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

– Arne Uten
Nov 14 '18 at 15:44







I'm using lxml and from that library I'm using etree. The tutorial I'm referring to comes from another thread and the link is lxml.de/tutorial.html

– Arne Uten
Nov 14 '18 at 15:44















Perhaps this helps: stackoverflow.com/q/40410923/407651

– mzjn
Nov 14 '18 at 15:56





Perhaps this helps: stackoverflow.com/q/40410923/407651

– mzjn
Nov 14 '18 at 15:56













Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

– Arne Uten
Nov 14 '18 at 16:14







Thanks @mzjn ! I took a first glance on the 'fix' that was found but as he also mentions after his try-out, it is not an optimal solution and I would like to keep it as clean as possible.

– Arne Uten
Nov 14 '18 at 16:14















It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

– mzjn
Nov 14 '18 at 16:34





It's tricky. There have been many questions about pretty-printing XML. This one has 19 answers, stackoverflow.com/q/749796/407651, but I'm not sure if they are of any help in this case (arranging attributes).

– mzjn
Nov 14 '18 at 16:34













You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

– mzjn
Nov 14 '18 at 16:42





You might want to try the Tidy utility (html-tidy.org). It has a indent-attributes option: api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes.

– mzjn
Nov 14 '18 at 16:42












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303684%2flxml-etree-pretty-print-attributes-of-root-tag%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303684%2flxml-etree-pretty-print-attributes-of-root-tag%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma