简体   繁体   中英

remove <?xml version="1.0" ?> using xml.dom.minidom

I am generating XML files using xml.dom.minidom . Every time I generate a file on the very row there appears <?xml version="1.0" ?> and the generated file looks like this:

<?xml version="1.0" ?> 
 <Root>
     data 
 </Root>

is not there anyway so have an output without and my output should look like

 <Root>
      data 
 </Root>

If you are happy just to trim the first line from the file, use this code;

f = open( 'file.txt', 'r' )
lines = f.readlines()
f.close()

f = open( 'file.txt'.'w' )
f.write( '\n'.join( lines[1:] ) )
f.close()

The best solution I found was to write out .childNodes[0] , ie write out:

doc.childNodes[0].toprettyxml()

to the file, which will omit the xml version tag.

这完成了 old_data 是要剥离的 xml 的工作

new_data = old_data[old_data.find("?>")+2:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM