#!/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")
这很好用,但是如果一个字符串以<b>
开头,它会严重破坏。 EX
<string name="uploading_to"><b>%s</b> Odovzdávanie do</string>
变
<string name="uploading_to" />
我查看了xml.etree Element类,但它似乎只有.text方法。我只需要一种方法来拉取xml标签之间的所有内容。不,我无法更改输入数据。它直接来自Android APK准备翻译,除了必须是有效的XML Android代码之外,我无法预测数据的来源/内容。
我认为你正在寻找itertext()
方法。 .text
只返回元素开头直接包含的文本:
>>> test = ET.fromstring('<elem>Sometext <subelem>more text</subelem> rest</elem>')
>>> test.text
'Sometext '
>>> ''.join(test.itertext())
'Sometext more text rest'
另一方面,.itertext()
迭代器让你找到元素中包含的所有文本,包括嵌套元素内部。
但是,如果您只希望文本直接包含在元素中,跳过包含的子元素,则需要组合每个子元素的.text
和.tail
值:
>>> (test.text or '') + ''.join(child.tail for child in test.getchildren())
'Sometext middle rest'
如果你需要捕获所有内容,那么你需要做更多的工作;捕获.text
,并用ElementTree.tostring()
将每个孩子投射到文本:
>>> (test.text or '') + ''.join(ET.tostring(child) for child in test.getchildren())
'Sometext <subelem>more text</subelem> middle <subelem>other text</subelem> rest'
ET.tostring()
考虑了元素尾部。我使用(test.text or '')
因为.text
属性也可以是None
。
您可以捕获函数中的最后一个方法:
def innerxml(elem):
return (elem.text or '') + ''.join(ET.tostring(child) for child in elem.getchildren())