gpt4 book ai didi

android - 使用 Ruby 脚本更新 Android Strings.xml

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:58 24 4
gpt4 key购买 nike

我想使用基于 Ruby 的脚本遍历 Android 中的 strings.xml 文件来更新某些值。例如:这是原始的xml文件

<resources>
<string name="accounts">accounts</string>
</resources>

我希望它在运行 ruby​​ 脚本后变成这样:

<resources>
<string name="accounts">my accounts</string>
</resources>

我对 ruby​​ 完全陌生,但我能够让它读取 xml 文件....只是不确定如何更新值。

(如果您想知道,我这样做是为了给我的应用贴上白标签并将其出售给企业。这将有助于大大加快流程。)

最佳答案

我找到了一种方法。

  require 'rubygems'
require 'nokogiri'

#opens the xml file
io = File.open('/path/to/my/strings.xml', 'r')
doc = Nokogiri::XML(io)
io.close

#this line looks for something like this: "<string name="nameOfStringAttribute">myString</string>"
doc.search("//string[@name='nameOfStringAttribute']").each do |string|

#this line updates the string value
string.content = "new Text -- IT WORKED!!!!"

#this section writes back to the original file
output = File.open('/path/to/my/strings.xml', "w")
output << doc
output.close

end

关于android - 使用 Ruby 脚本更新 Android Strings.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com