【问题标题】:how to parse Android string xml and change the key to the another key which has same value如何解析Android字符串xml并将密钥更改为另一个具有相同值的密钥
【发布时间】:2020-12-06 09:48:21
【问题描述】:

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

我有一个主机 Android 应用(名为 Host)和一个 Android 库(aar,名为 Library)。 Host 有一个名为strings_translatable.xml 的文件,Library 有一个名为string.xml 的文件。

众所周知,android字符串xml文件长这样:

<resources>
    <string name="all_cards">All Cards</string>
    <string name="mpi_title_add_card">Add Card</string>
</resoures>

我们假设 all_cards 称为 key,All Cards 称为 value。

我的问题是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?>

【问题讨论】:

  • 到目前为止,您做了哪些尝试?你被困在哪里了?请一次问一个问题。
  • 遍历string.xml中的所有key,找到strings_translatable.xml中对应的key
  • @mzjn 这是一个肯定的问题。我一直在寻找string.xml 中的所有字符串值
  • @deadshot 是的,但是如何实现呢?能否麻烦您提出一个解决方案?
  • 所以你还没有写任何代码?你应该从这里开始:docs.python.org/3/library/…

标签: python android xml xml-parsing


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    另一种方法。

    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?>
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 2012-08-02
      • 1970-01-01
      • 2015-11-02
      • 2011-04-14
      • 2011-03-21
      • 1970-01-01
      • 2015-11-17
      • 2022-06-14
      相关资源
      最近更新 更多