2013-08-11 5 views
6

मेरे पास एक एक्सएमएल है जो मैं पार्सिंग कर रहा हूं, कुछ बदलाव कर रहा हूं और एक नई फाइल में सहेज रहा हूं। इसमें घोषणा <?xml version="1.0" encoding="utf-8" standalone="yes"?> है जिसे मैं रखना चाहता हूं। जब मैं अपनी नई फाइल को सहेज रहा हूं तो मैं 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')) 

उत्तर

12

आप tostring() करने के लिए standalone कीवर्ड तर्क पारित कर सकते हैं:

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

लेखन त्रुटि: toString() एक अप्रत्याशित कीवर्ड तर्क 'xml_declaration' –

+0

@ArnoldRoa आप 'lxml.etree' का उपयोग कर रहे है? – alecxe

7

tree.docinfo.standalone का उपयोग कर standalone निर्दिष्ट यहाँ मेरी कोड है।

निम्न प्रयास करें:

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

क्षमा करें, आपका उत्तर एक आकर्षण की तरह काम करता है, मैंने सोचा कि @alecxe उत्तर मेरे लिए लागू करना आसान था, वैसे भी आपके उत्तर के लिए धन्यवाद, विकल्पों के लिए अच्छा है। – speedyrazor

+0

@ user2446702, ठीक है, मैं देखता हूं। – falsetru

2

आप अपनी XML शीर्षक में standalone='no' तर्क को दिखाने के लिए चाहते हैं, आप के बजाय 'नहीं' False के लिए सेट करने के लिए है। बस इस तरह:

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

यदि नहीं, तो स्टैंडअलोन डिफ़ॉल्ट रूप से 'हाँ' पर सेट हो जाएगा।

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

आप lxml उपयोग कर रहे हैं घोषणा जोड़ देगा, लेकिन मैं उनकी घोषणा पूर्ण उद्धरण के बजाय अर्द्ध उद्धरण का उपयोग करता देखा।

तुम भी सटीक घोषणा तुम सिर्फ एक स्थिर स्ट्रिंग आप की जरूरत के साथ उत्पादन श्रृंखलाबद्ध द्वारा चाहते हैं प्राप्त कर सकते हैं:

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

आप सब पर None बजाय True या False पारित standalone outputting अक्षम करना चाहते हैं। तार्किक लगता है लेकिन वास्तव में खोजने और परीक्षण करने के लिए कुछ समय लगा।

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

या संदर्भ प्रबंधक और धारा etree.xmlfile क्रमबद्धता का उपयोग कर:

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) 
संबंधित मुद्दे