#!/usr/bin/env python
import os, sys, os.path
import string
def sort_strings_file(xmlfile,typee):
"""sort all strings within given strings.xml file"""
all_strings = {}
orig_type=typee
# read original file
tree = ET.ElementTree()
tree.parse(xmlfile)
# iter over all strings, stick them into dictionary
for element in list(tree.getroot()):
all_strings[element.attrib['name']] = element.text
# create new root element and add all strings sorted below
newroot = ET.Element("resources")
for key in sorted(all_strings.keys()):
# Check for IDs
if typee == "id":
typee="item"
# set main node type
newstring = ET.SubElement(newroot, typee)
#add id attrib
if orig_type == "id":
newstring.attrib['type']="id"
# continue on
newstring.attrib['name'] = key
newstring.text = all_strings[key]
# write new root element back to xml file
newtree = ET.ElementTree(newroot)
newtree.write(xmlfile, encoding="UTF-8")
これはうまく機能しますが、文字列が like で始まると、<b>
ひどく壊れます。元
<string name="uploading_to"><b>%s</b> Odovzdávanie do</string>
になる
<string name="uploading_to" />
xml.etree Element クラスを調べたのですが、.text メソッドしかないようです。xml タグの間にすべてを取り込む方法が必要なだけです。いいえ、入力データを変更できません。それは、翻訳する準備ができている Android APK から直接取得されます。有効な XML Android コードでなければならないという事実以外に、データがどのように/何を受け取るかを予測することはできません。