Skip to content

Instantly share code, notes, and snippets.

@CNBorn
Created May 1, 2014 03:01
Show Gist options
  • Save CNBorn/b30b8cc3a32187c40a01 to your computer and use it in GitHub Desktop.
Save CNBorn/b30b8cc3a32187c40a01 to your computer and use it in GitHub Desktop.
#encoding: utf-8
import xml.etree.ElementTree as ET
tree = ET.parse('angkorwat.xml')
root = tree.getroot()
list_title = []
list_image = []
list_image_author = []
list_image_copyright_sign = []
list_desc = []
for strings in root.findall('string-array'):
print strings.attrib
for item in strings.findall('item'):
if strings.attrib['name'].endswith('_images'):
list_image.append(item.text)
elif strings.attrib['name'].endswith('_image_copyrights'):
list_image_author.append(item.text)
elif strings.attrib['name'].endswith('_image_copyright_signs'):
list_image_copyright_sign.append(item.text)
elif strings.attrib['name'].endswith('_picture_titles'):
list_title.append(item.text)
elif strings.attrib['name'].endswith('_descriptions'):
list_desc.append(item.text)
print list_title
resources = ET.Element('resources')
for idx, title in enumerate(list_title):
slide = ET.SubElement(resources, 'slide')
title = ET.SubElement(slide, 'title')
title.text = list_title[idx]
image = ET.SubElement(slide, 'image')
image.text = list_image[idx]
image_author = ET.SubElement(slide, 'image_author')
image_author.text = list_image_author[idx]
image_copyright = ET.SubElement(slide, 'image_copyright_sign')
image_copyright.text = list_image_copyright_sign[idx]
desc = ET.SubElement(slide, 'desc')
desc.text = list_desc[idx]
empty = ET.SubElement(slide, 'empty')
import codecs
out = codecs.open("angkorwat_xml.xml", "w", "utf-8")
from xml.dom import minidom
def newwritexml(self, writer, indent= '', addindent= '', newl= ''):
if len(self.childNodes)==1 and self.firstChild.nodeType==3:
writer.write(indent)
self.oldwritexml(writer) # cancel extra whitespace
writer.write(newl)
else:
self.oldwritexml(writer, indent, addindent, newl)
minidom.Element.oldwritexml= minidom.Element.writexml
minidom.Element.writexml = newwritexml
r = minidom.parseString(ET.tostring(resources))
r.encoding="UTF-8"
r.writexml(out, indent=" ", newl="\n", encoding="UTF-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment