Ladislav M
Ladislav M

Reputation: 2187

Update value of XML attributes using Shell

I have config.xml with following line inside.

<widget id="dev.apk" version="2.5-dev" android-versionCode="20500-dev">

I'd like to change value of attributes 'version', 'id', and 'android-versionCode' in my Bash script.

In the script I have access to ID, VERSION an VERSION_CODE variables, like this:

ID ="release.apk"
VERSION="2.5"
VERSION_CODE="20500"

I'd like to use xmlstarlet or sed. What would be the best way?

Update

My .xml file looks like this:

<?xml version="1.0" encoding="utf-8"?> 
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" id="app" version="2.5" android-versionCode="20500"> 
<name>App title</name> 
<description>Dummy dummy text.</description> 
</widget>

Upvotes: 2

Views: 1609

Answers (2)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185690

You can do this to edit in-place the xml file :

Example for id attribute :

xmlstarlet edit -L -u "/_:widget/@id" -v "$ID" file.xml

You just have now to adapt this snippet for the other ones.

NOTE

  • The _: in the Xpath expression is for taking care of the XML namespace

Upvotes: 2

shaine
shaine

Reputation: 549

You could do something like the below in sed.

sed -i "s|dev.apk|$ID|g" test.xml

But if you are already using a build tool you might want to have a look at replacing your values with placeholder and having targeted environment files

I believe that there is some placeholder support in gradle

http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger

Upvotes: 0

Related Questions