0

I have the following xml file

<root> <deviceID>IO238470374109730497</devicdeID> <KeyValuePairs> <KeyValuePair> <BASE-URL>site.com</BASE-URL> <Key>329482</Key> <Value>23094</Value> </KeyValuePair> <KeyValuePair> <BASE-URL>anothersite.com</BASE-URL> <Key>36657</Key> <Value>1233</Value> </KeyValuePair> <KeyValuePair> <BASE-URL>yetanothsite.com</BASE-URL> <Key>3345</Key> <Value>65087</Value> </KeyValuePair> </KeyValuePairs> </root>

I would like to extract the values of all <BASE-URL>,<Key>,<Value> and set them to variables in a bash script.

executing this command will only print the values

xmlstarlet sel -t -m '//KeyValuePair' -v 'concat(BASE-URL," ", Key, " ", Value, " ")' -nl file.xml

How can I set these values to variables?

I want to extract the values from the xml and send them to variables like this <BASE-URL> to $base_url , <Key> to $key and <Value> to $value.

2

Assuming the values never contain spaces you can do something like this:

xmlstarlet sel -t -m '//KeyValuePair' -v 'concat(BASE-URL," ", Key, " ", Value, " ")' -nl file.xml | while read base_url key value
do
    echo "base_url=$base_url key=$key value=$value"
done
  • It works.. you saved my day.. thank you very much! – Mustafa W Alani Feb 13 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.