如何解析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?>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...