How to write an XML file without header in Python?

后端 未结 7 1285
盖世英雄少女心
盖世英雄少女心 2021-02-06 01:28

when using Python\'s stock XML tools such as xml.dom.minidom for XML writing, a file would always start off like

<

相关标签:
7条回答
  • 2021-02-06 01:48

    You might be able to use a custom file-like object which removes the first tag, e.g:

    class RemoveFirstLine:
        def __init__(self, f):
            self.f = f
            self.xmlTagFound = False
    
        def __getattr__(self, attr):
            return getattr(self, self.f)
    
        def write(self, s):
            if not self.xmlTagFound:
                x = 0 # just to be safe
                for x, c in enumerate(s):
                    if c == '>':
                        self.xmlTagFound = True
                        break
                self.f.write(s[x+1:])
            else:
                self.f.write(s)
    
    ...
    f = RemoveFirstLine(open('path', 'wb'))
    Node.writexml(f, encoding='UTF-8')
    

    or something similar. This has the advantage the file doesn't have to be totally rewritten if the XML files are fairly large.

    0 讨论(0)
  • 2021-02-06 01:52

    Unfortunately minidom does not give you the option to omit the XML Declaration.

    But you can always serialise the document content yourself by calling toxml() on the document's root element instead of the document. Then you won't get an XML Declaration:

    xml= document.documentElement.toxml('utf-8')
    

    ...but then you also wouldn't get anything else outside the root element, such as the DOCTYPE, or any comments or processing instructions. If you need them, serialise each child of the document object one by one:

    xml= '\n'.join(node.toxml('utf-8') for node in document.childNodes)
    

    I wondered if there are other packages which do allow to neglect the header.

    DOM Level 3 LS defines an xml-declaration config parameter you can use to suppress it. The only Python implementation I know of is pxdom, which is thorough on standards support, but not at all fast.

    0 讨论(0)
  • 2021-02-06 01:53

    If you want to use minidom and maintain 'prettiness', how about this as a quick/hacky fix:

    xml_without_declaration.py:

    import xml.dom.minidom as xml
    
    doc = xml.Document()
    
    declaration = doc.toxml()
    
    a = doc.createElement("A")
    doc.appendChild(a)
    b = doc.createElement("B")
    a.appendChild(b)
    
    xml = doc.toprettyxml()[len(declaration):]
    
    print xml
    
    0 讨论(0)
  • 2021-02-06 01:57

    Just replace the first line with blank:

    import xml.dom.minidom as MD     
    <XML String>.replace(MD.Document().toxml()+'\n', '') 
    
    0 讨论(0)
  • 2021-02-06 01:58

    Purists may not like to hear this, but I have found using an XML parser to generate XML to be overkill. Just generate it directly as strings. This also lets you generate files larger than you can keep in memory, which you can't do with DOM. Reading XML is another story.

    0 讨论(0)
  • 2021-02-06 02:05

    The header is print in Document. If you print the node directly, it won't print the header.

    root = doc.childNodes[0]
    root.toprettyxml(encoding="utf-8")
    
    0 讨论(0)
提交回复
热议问题