Parse XML using xmllint


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse XML using xmllint
# 1  
Old 12-23-2013
Parse XML using xmllint

Hi All,

Need help to parse the xml file in shell script using xmllint. Below is the sample xml file.

Code:
<CARS>
 <AUDI>
  <Speed="45"/>
  <speed="55"/>
  <speed="75"/>
  <speed="95"/>
 </AUDI>
 
 <BMW>
  <Speed="30"/>
  <speed="75"/>
  <speed="120"/>
  <speed="135"/>
 </BMW>
</CARS>

From above xml file, i need to get all the speed values of BMW and assign it to a variable which will be used in the shell script. I need to use xmllint to achieve this..

Thanks in advance...
# 2  
Old 12-23-2013
Try

Code:
$ awk '/<Speed|<speed/ && gsub(/.*="|"\/>/,x)' file
45
55
75
95
30
75
120
135

Code:
$ grep -Po -i '(?<=<Speed=").*(?="/>)' file
45
55
75
95
30
75
120
135

# 3  
Old 12-23-2013
Your XML document is not well-formed, so you have to fix it before you can run xmllint
# 4  
Old 12-23-2013
Hello,

One more approach may help.

Code:
awk '/[0-9]/ {print $0}' file_name | sed 's/[a-zA-Z]//g;s/[[:punct:]]//g'

Output will be as follows.

Code:
  45
  55
  75
  95
  30
  75
  120
  135


Thanks,
R. Singh
# 5  
Old 12-23-2013
I noticed all previous solutions posted extract all speed values. But OP is interested in extracting speed values only for BMW and wanted to assign them to shell variable.

Here is an approach using bash, storing each speed values into an array:
Code:
#!/bin/bash

flag=0
while read line
do
        [[ "$line" =~ "<BMW>" ]] && flag=1
        [[ "$line" =~ "</BMW>" ]] && flag=0
        if [ $flag -eq 1 ] && [[ "$line" =~ [Ss]peed ]]
        then
                (( ++c ))
                line="${line##*=\"}"
                S[$c]="${line%%\"*}"
        fi
done < file.xml

for k in "${S[@]}"
do
        echo "$k"
done

# 6  
Old 12-23-2013
My apologies for 2nd post I didn't read requirement properly. Since requirement is only one array so we can also define like this

Code:
$ cat test.sh
#!/bin/bash

# Parse speed  values between tag and write output to temporary file
  awk '/<BMW>/,/<\/BMW>/{if(/<Speed|<speed/){gsub(/.*="|"\/>/,x);print}}' file.xml >test.tmp

# Create array
  arr=( $( < test.tmp  ) )

# OR you can use cut command for specific column into an Array instead of entire line may be useful in future
# arr=( $( < <(cut -f1 test.tmp )  ) )

# delete temporary file created using awk
  rm -f test.tmp

# Print Array Elements
  for i in $(seq 0 $((${#arr[@]} - 1)))
  do
    echo ${arr[$i]}
  done

# OR
# for i in ${arr[@]}
# do
#    echo $i
# done

Code:
$ bash test.sh
30
75
120
135


Last edited by Akshay Hegde; 12-23-2013 at 02:24 PM.. Reason: formatting...
# 7  
Old 01-29-2014
Hello,

Here one approach may help.

Code:
awk '/\<\/BMW\>/  {f=0} (f ~ 1) {gsub(/[[:alpha:]]/,X,$0) gsub(/[[:punct:]]/,Y,$0); {print $0}} /\<BMW\>/ {f=1}' check_all_BMW_values

Output will be as follows.


Code:
  30
  75
  120
  135


Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse XML For Values

Hi All, I want to parse XML to extract values of the tags to do further processing. The XML looks like <?xml version="1.0" encoding="ISO-8859-1"?> <allinput> <input A="2389906" B="install"> <C>111</C> <D>222</D> <E>333</E> <F></F> <G>444</G> <H></H> <I></I> <J></J> <K>C,D,E,G</K>... (6 Replies)
Discussion started by: rahulmittal87
6 Replies

2. Shell Programming and Scripting

Xmllint - Xml-file problem --ods file not opening

Dear All, this is my first post on this Forum, glad to be here. I'm trying to fix an .ods file. Yes, I had a backup, but it's also corrupted. When opening the document I get this EM: read error format error discovered in the file in sub-document content.xml at 2,337040(row,col). So I... (3 Replies)
Discussion started by: jameslast
3 Replies

3. Shell Programming and Scripting

Parse XML

Hi all! I'm looking to write a quick script and in it I need to request an XML file from a service running on localhost and parse that XML file and output it. I'm looking to do it in bash although it doesn't really matter what shell it is in. The XML file returned would look like this: ... (3 Replies)
Discussion started by: mtehonica
3 Replies

4. Shell Programming and Scripting

XMLLINT COMMAND IN UNIX TO VALIDATE XML AGAINST XSD

Hi i am baby to unix shell script. how do i validate xml agaist xsd and transforms xml using xslt. Thanks Mohan (2 Replies)
Discussion started by: mohan.cheepu
2 Replies

5. Shell Programming and Scripting

parse xml file

Hello all, Given the following extract from a xml file with multiple <JOB> .... </JOB> entries <JOB APPLICATION="APP" APR="0" AUG="0" AUTHOR="AUT" AUTOARCH="0" CMDLINE="/tmp/test1 %%var" CONFIRM="1" CREATION_DATE="20100430" CREATION_TIME="130739" ... (2 Replies)
Discussion started by: cabrao
2 Replies

6. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

7. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

8. Shell Programming and Scripting

Parse XML file

Hi, I need to parse the following XML data enclosed in <a> </a> XML tag using shell script. <X> ..... </X> <a> <b> <c>data1</c> <c>data2</c> </b> <d> <c>data3</c> </d> </a> <XX> ... </XX> (5 Replies)
Discussion started by: viki
5 Replies

9. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

10. Programming

parse xml

Hi, I'm looking for an "easy" way to parse a xml file to a proper structure. The xml looks like this What shall I use? Does anybody has some example-code to share or some good links/book-references? thx for any reply -fe (5 Replies)
Discussion started by: bin-doph
5 Replies
Login or Register to Ask a Question