Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preserve xml file format
#1
Hi!
I would like to manipulate an xml file yet preserve it's format (extra spaces, comments, cr, ..).
lxml preserve comments out of the box but was unable to figure out the rest.

My current solution is to parser the file line by line..

For example:
<node name="test_basic" size="0x200" >
<field name="aaa" offset="0x0.0" size="0x100.0" subnode="xxx" descr="" />
<field name="bbb" offset="++" size="0x1.0" subnode="xxx" descr="" />
<field name="ccc" offset="++" size="0x1.2" subnode="xxx" descr="" />
</node>

<!-- test comment-->


<node name="test_bits" size="?" >
<field name="aaa" offset="0x0.0" size="0.1" descr="" />
<field name="bbb" offset="++" size="0x1.1" descr="" />
</node>
Reply
#2
Please show what you have tried so far.
Reply
#3
The formatting were removed by the forum editor as well :/

Should have looked like this:
  <field name="aaa"                     offset="0x0.0"      size="0.1"    subnode="xxx" descr="" />
The code to test the preservation was: (And a lot of googling)
#import xml.etree.ElementTree as ET
import lxml.etree as ET

tree = ET.parse('test.xml')
tree.write('new.xml')
The output (new.xml) no longer contains spaces.
Reply
#4
I believe that you can parse pure XML with BeautifulSoup, using 'lxml' as the parser without modifying the spacing.
I use the same setup all the time for html and haven't noticed any change in format.
It's been a while since I used lxml.etree directly. As I recall it was temperamental.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python-docx: preserve formatting when printing lines Tmagpy 4 2,092 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  Reshape txt file into particular format using python shantanu97 0 1,423 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  How can we transcode encoding file uml url format Anldra12 9 3,379 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to design a save file format? philipbergwerf 5 4,130 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  tabula-py, how to preserve a read_pdf() format and export to csv abcoelho 2 3,282 Mar-24-2021, 08:34 PM
Last Post: abcoelho
  How to preserve x-axis labels despite deleted subplot? Mark17 1 1,922 Dec-23-2020, 09:02 PM
Last Post: Mark17
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,462 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,007 Jul-22-2020, 06:06 PM
Last Post: Larz60+
  copy/pasting in excel WHILE keep file format zarize 0 1,955 Jun-23-2020, 03:51 PM
Last Post: zarize
  Load and format a CSV file fioranosnake 11 4,503 Oct-30-2019, 12:32 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020