SAE python处理xml文件

# -*- coding: utf-8 -*-
import sae.const  
import StringIO  
import urllib  
import xml.etree.ElementTree as ET


class XmlParse:  
    @staticmethod
    def parse_job():
        url = "http://clantest-data.stor.sinaapp.com/test.xml"
        f = urllib.urlopen(url)
        my_file = f.read()
        return my_file

    def xml_read(self, content):
        in_mem_file = StringIO.StringIO(content)
        # tree = ET.fromstring(content)
        tree = ET.parse(in_mem_file)
        root = tree.getroot()
        b = ET.SubElement(root, "child")
        b.text = "Jay/Doctor"
        tmp_file = StringIO.StringIO("")
        tree.write(tmp_file)
        return tmp_file.getvalue()
  • parse_job: 实现从URL读取一个标准的xml内容
  • xml_read 修改xml,注意tree = ET.fromstring(content)也可以不使用StringIO,直接从字符串生成一个ElementTree 对象。