如何解决如何解析Android字符串XML并将密钥更改为具有相同值的另一个密钥
如何解析Android字符串xml并完成Android库的本地化
我有一个主机Android应用(名为Host)和一个Android库(aar,名为Library)。
主机有一个名为strings_translatable.xml
的文件,而库有一个名为string.xml
的文件。
众所周知,android字符串xml文件如下所示:
<resources>
<string name="all_cards">All Cards</string>
<string name="mpi_title_add_card">Add Card</string>
</resoures>
我们假设all_cards称为键,而All Cards称为值。
我的问题是:
当它们具有相同的值时,如何将string.xml
中的密钥更改为strings_translatable.xml
中的密钥?
第1步:
如何在文件string.xml
中打印Python中的所有值和键?
第2步:
如何在string.xml
中找到与strings_translatable.xml
中相同的值?
第3步:
当它们具有相同的值时,如何将string.xml
中的密钥更改为strings_translatable.xml
中的密钥?
欢迎对这些步骤进行任何解决。
更新:
样本数据:
string.xml
<resources>
<string name="a">About Us</string>
<string name="b">Reactivate Card</string>
<string name="c">Accounts linked to</string>
</resources?>
strings_translatable.xml
<resources>
<string name="d">About Us</string>
<string name="e">Reactivate Card</string>
<string name="f">Other value which is not included in string.xml</string>
</resources?>
解决方法
从strings_translatable.xml
此处创建字典,键是字符串值,而value是字符串名称。
示例字典如下
{'About Us': 'd','Reactivate Card': 'e','Other value which is not included in string.xml': 'f'}
然后遍历string.xml
中的每个值,如果字典中存在字符串值,则使用set()方法更新字符串名称。
import xml.etree.ElementTree as ET
tree1 = ET.parse('string.xml')
root1 = tree1.getroot()
tree2 = ET.parse('strings_translatable.xml')
root2 = tree2.getroot()
d = {x.text: x.attrib['name'] for x in root2.findall('string') if x.text}
for x in root1.findall('string'):
key = x.text
if key in d:
x.set('name',d[key])
tree1.write('new_string.xml')
输出(new_string.xml)
<resources>
<string name="d">About Us</string>
<string name="e">Reactivate Card</string>
<string name="c">Accounts linked to</string>
</resources>
,
另一种方法。
from simplified_scrapy import SimplifiedDoc,utils
# html = utils.getFileContent('string.xml')
string = '''
<resources>
<string name="a">About Us</string>
<string name="b">Reactivate Card</string>
<string name="c">Accounts linked to</string>
</resources?>
'''
# strings_translatable = utils.getFileContent('strings_translatable.xml')
strings_translatable = '''
<resources>
<string name="d">About Us</string>
<string name="e">Reactivate Card</string>
<string name="f">Other value which is not included in string.xml</string>
</resources?>
'''
docString = SimplifiedDoc(string)
dicString = {}
for string in docString.selects('string'):
# dicString[string['name']]=string['text']
dicString[string.text] = string # If the value does not repeat
docTranslatable = SimplifiedDoc(strings_translatable)
for string in docTranslatable.selects('string'):
node = dicString.get(string.text) # find a value in string.xml which has the same value in strings_translatable.xml
if node:
node.setAttr('name',string['name']) # change the key in string.xml to the key in strings_translatable.xml when they have the same value
utils.saveFile('string.xml',docString.html) # update string.xml
结果:
<resources>
<string name="d">About Us</string>
<string name="e">Reactivate Card</string>
<string name="c">Accounts linked to</string>
</resources?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。