如何解析Android字符串xml并将该键更改为另一个具有相同值的键

2024-05-13 23:51:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何解析Android字符串xml并完成Android库的本地化

我有一个主机Android应用程序(命名主机)和一个Android库(aar,命名库)。 主机有一个名为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>

我们假设所有的卡片都称为键,所有的卡片都称为值

我的问题是: 当它们具有相同的值时,如何将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?>

字符串\u translateable.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?>

Tags: 文件字符串namestring步骤xmlcard命名
2条回答

strings_translatable.xml创建了一个字典,其中key是字符串值,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?>

相关问题 更多 >