如何使用bash更改xml中的值

时间:2016-03-08 09:16:07

标签: xml bash

如何使用bash

更改xml中的值

<resources> <string name="app_name">Keep Accounts</string> <string name="login">"login"</string> <string name="login_password">"password:"</string> <string name="login_account_hint">input to login</string> <string name="login_password_hint">input your password</string> <string name="login_fail">login failed</string> </resources>

例如,我想更改值&#34; Keep Accounts&#34; to&#34; Keep Accounts 2&#34;。&#34; app_name&#34;可能不仅仅是&#34;保持帐户&#34;,还有其他价值,让我们说XXX,但&#34; app_name&#34;可以肯定的是,我希望输出为:

<resources> <string name="app_name">XXX 2</string> <string name="login">"login"</string> <string name="login_password">"password:"</string> <string name="login_account_hint">input to login</string> <string name="login_password_hint">input your password</string> <string name="login_fail">login failed</string> </resources>

3 个答案:

答案 0 :(得分:1)

使用XML感知工具。我在xsh

中执行此操作
  public static string CleanupStringForValidXMLExport(this string inputString)
    {
        StringBuilder s = new StringBuilder();
        foreach(char ch in inputString)
        {
            if (XmlConvert.IsXmlChar(ch))
                s.Append(ch);

        }

        return s;
    }

答案 1 :(得分:0)

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if pickerView == self.dayPicker{
      print("picked \(dayPickerDataSource[row])")
    }else {
      print("picked \(durationPickerDataSource[row])")
    }

  }

替换任何'app_name'

的单行sed(bash)解决方案

使用sed -i替换有问题的文件的内容

我假设您在名为xml_filename.xml的文件中有xml内容,但是根据需要更改了

答案 2 :(得分:0)

如果您安装了python-pip,则可以通过pip install yq安装xq,然后尝试执行此操作(我已经测试了它的工作原理):

xmldoc=$(cat <<'EOF'
<resources>
    <string name="app_name">Keep Accounts</string>
    <string name="login">"login"</string>
    <string name="login_password">"password:"</string>
    <string name="login_account_hint">input to login</string>
    <string name="login_password_hint">input your password</string>
    <string name="login_fail">login failed</string>
</resources>
EOF
)
echo "$xmldoc" | xq '.resources.string = ([.resources.string[]|select(."#text" == "Keep Accounts") ."#text" = "Keep Accounts 2"])' -x