2011-07-12 65 views
1

我目前正在用xml.dom.minidom構建大型xml文件,然後通過toprettyxml將它們寫入文件。有沒有辦法將XML流式傳輸到文檔,因爲我正在記錄錯誤。HDI:將大字符串xml寫入文件(python xml.dom.minidom)

def run(self): 
    while True: 
     domain = self.queue.get() 
     try: 
      conn = boto.connect_sdb(awsa, awss) 
      sdbdomain = conn.get_domain(domain) 
      s3conn = boto.connect_s3(awsa, awss) 
      archbucket = s3conn.get_bucket("simpledbbu") 
      doc = None 
      doc = Document() 
      root = doc.createElement("items") 
      doc.appendChild(root) 
      countermax = 0 
      counter = 0 
      for item in sdbdomain: 
       node = doc.createElement("item") 
       node.setAttribute("itemName", item.name) 
       for k,v in item.items(): 
        if not isinstance(v, basestring): 
         i = 0 
         for val in v: 
          node.setAttribute("{0}::{1}".format(k,i),val) 
          i += 1 
        else: 
         node.setAttribute(k,v) 
       root.appendChild(node) 
      k = Key(archbucket) 
      k.key = "{0}/{1}.xml".format(datetime.date.today().strftime("%Y%m%d"),sdbdomain.name) 
      #x = doc.toprettyxml(indent=" ") 
      f = open(domain + ".xml", "w") 
      f.truncate() 
      f.write(doc.toprettyxml(indent=" ")) 
      f.close() 
      #k.content_type.encode('ascii') 
      k.set_contents_from_filename(f.name) 
      os.remove(os.path.join(os.getcwd(),f.name)) 
     except: 
      print "failed to load domain: {0}".format(domain) 
      print formatExceptionInfo() 
     finally: 
      self.queue.task_done() 

回答

1

建設有xml.dom.minidom大的XML文件,然後寫出來通過toprettyxml到文件。

如果內存不足,您應該停止這樣做。

您可以使用簡單的字符串操作來構建XML。

with open(domain + ".xml", "w") as f: 
    f.write("<?xml...") 
    f.write("<items>") 
    for item in sdbdomain: 
     buffer= [] 
     for k,v in item.items(): 
      if not isinstance(v, basestring): 
      for i, val in enumerate(v): 
       txt= '{0}::{1}="{2}"'.format(k,i,val) 
      else: 
      txt= '{0}="{1}"'.format(k,v) 
      buffer.append(txt) 
     f.write(" <item {0}/>\n".format(" ".join(buffer))) 
    f.write("</items>") 
k= ................  
k.set_contents_from_filename(f.name) 

類似的東西應該允許您將XML寫入臨時文件而不在內存中生成大型DOM對象。

+0

是的,只是寫出了XML,這是簡單的XML。謝謝 –

+0

@Chris:說到寫*,所有的XML都很簡單。 DOM模型比解析更適合創建。 –