मेरे पास xml.dom.minidom के साथ कुछ XML टेक्स्ट उत्पन्न करने के लिए कुछ पायथन कोड है। अभी, मैं इसे टर्मिनल से चलाता हूं और परिणामस्वरूप मुझे एक संरचित एक्सएमएल आउटपुट करता है। मैं इसे एक एक्सएमएल फाइल भी उत्पन्न करना चाहता हूं और इसे अपनी डिस्क पर सहेजना चाहता हूं। यह कैसे किया जा सकता है?एक एक्सएमएल फ़ाइल को पाइथन के साथ डिस्क में कैसे सहेजना है?
import xml
from xml.dom.minidom import Document
import copy
class dict2xml(object):
doc = Document()
def __init__(self, structure):
if len(structure) == 1:
rootName = str(structure.keys()[0])
self.root = self.doc.createElement(rootName)
self.doc.appendChild(self.root)
self.build(self.root, structure[rootName])
def build(self, father, structure):
if type(structure) == dict:
for k in structure:
tag = self.doc.createElement(k)
father.appendChild(tag)
self.build(tag, structure[k])
elif type(structure) == list:
grandFather = father.parentNode
tagName = father.tagName
# grandFather.removeChild(father)
for l in structure:
tag = self.doc.createElement(tagName.rstrip('s'))
self.build(tag, l)
father.appendChild(tag)
else:
data = str(structure)
tag = self.doc.createTextNode(data)
father.appendChild(tag)
def display(self):
print self.doc.toprettyxml(indent=" ")
यह सिर्फ एक्सएमएल उत्पन्न करता है:
यह है कि मैं क्या है। मैं इसे अपने डेस्कटॉप पर फ़ाइल के रूप में कैसे सहेज सकता हूं?
अभी भी इसे काम करने के लिए प्रबंधित नहीं कर सकता –
आपने वास्तव में क्या किया कोशिश करें? आपके संपादित प्रश्न में केवल एक्सएमएल को कंसोल पर प्रिंट करने के लिए कोड शामिल है। इसमें 'writexml()' का उपयोग करने का आपका प्रयास शामिल नहीं है, इसलिए हम इसे ठीक करने में आपकी सहायता नहीं कर सकते हैं। कृपया हमें दिखाएं कि आपने इसका उपयोग करने का प्रयास कैसे किया 'writexml() 'और यह कैसे काम करने में असफल रहा (क्या कोई त्रुटि संदेश था? या क्या यह आपके काम के रूप में काम करने में असफल रहा?) –
मोड में खोलना" wb "मेरे लिए काम नहीं करता है, हालांकि सिर्फ" डब्ल्यू "काम करता है –