2013-08-11 42 views
6

Tôi có một xml tôi đang phân tích cú pháp, thực hiện một số thay đổi và lưu vào một tệp mới. Nó có tuyên bố <?xml version="1.0" encoding="utf-8" standalone="yes"?> mà tôi muốn giữ. Khi tôi lưu tập tin mới của mình, tôi sẽ mất bit standalone="yes". Làm thế nào tôi có thể giữ nó trong? Đây là mã của tôi:XML Declaration standalone = "yes" lxml

templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<package> 
    <provider>Some Data</provider> 
    <studio_display_name>Some Other Data</studio_display_name> 
</package>""" 

from lxml import etree 
tree = etree.fromstring(templateXml) 

xmlFileOut = '/Users/User1/Desktop/Python/Done.xml' 

with open(xmlFileOut, "w") as f: 
    f.write(etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8')) 

Trả lời

12

Bạn có thể vượt qua standalone tranh cãi từ khóa để tostring():

etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone="yes") 
+1

Lỗi Loại: toString() có một từ khóa bất ngờ lập luận 'xml_declaration' –

+0

@ArnoldRoa được bạn sử dụng 'lxml.etree'? – alecxe

7

Chỉ định standalone sử dụng tree.docinfo.standalone.

Hãy thử sau:

from lxml import etree 
tree = etree.fromstring(templateXml).getroottree() # NOTE: .getroottree() 

xmlFileOut = '/Users/User1/Desktop/Python/Done.xml' 

with open(xmlFileOut, "w") as f: 
    f.write(etree.tostring(tree, pretty_print=True, xml_declaration=True, 
          encoding=tree.docinfo.encoding, 
          standalone=tree.docinfo.standalone)) 
+0

Xin lỗi, câu trả lời của bạn đã hoạt động như một sự quyến rũ, tôi chỉ nghĩ rằng câu trả lời @alecxe dễ thực hiện hơn cho tôi, cảm ơn bạn vì câu trả lời của bạn, rất tốt để có các tùy chọn. – speedyrazor

+0

@ user2446702, OK, tôi hiểu. – falsetru

2

Nếu bạn muốn hiển thị standalone='no' lập luận trong phần đầu XML của bạn, bạn phải thiết lập nó để False thay vì 'không'. Chỉ cần như thế này:

etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=False) 

Nếu không, độc lập sẽ được đặt thành 'có' theo mặc định.

0
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8') 

Sẽ thêm khai báo nếu bạn đang sử dụng lxml, tuy nhiên tôi nhận thấy tuyên bố của họ sử dụng dấu ngoặc kép thay vì báo giá đầy đủ.

Bạn cũng có thể nhận được việc kê khai chính xác mà bạn muốn bằng cách chỉ concatenating đầu ra với một chuỗi tĩnh bạn cần:

xml = etree.tostring(tree, pretty_print = True, encoding='UTF-8') 
xml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>\n' + xml 
0

Nếu bạn muốn vô hiệu hóa xuất ra standalone ở tất cả vượt qua None thay vì True hoặc False. Nghe có vẻ hợp lý nhưng phải mất chút thời gian để tìm và thử nghiệm nó.

etree.tostring(tree, xml_declaration = True, encoding='utf-8', standalone=None) 

hoặc sử dụng quản lý và bối cảnh dòng etree.xmlfile serialization:

with etree.xmlfile(open('/tmp/test.xml'), encoding='utf-8') as xf: 
    xf.write_declaration(standalone=None) 
    with xf.element('html'): 
     with xf.element('body'): 
      element = etree.Element('p') 
      element.text = 'This is paragraph' 
      xf.write(element)