# Parse XML file

**URL:** <https://community.unix.com/t/parse-xml-file/170482>\
**Category:** Shell Programming and Scripting\
**Created:** [April 12, 2007, 10:12am UTC](https://community.unix.com/t/parse-xml-file/170482 "2007-04-12T10:12:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![viki](https://community.unix.com/letter_avatar/viki/32/6_22734026db12803ebb3e059622ac3534.png) [@viki](https://community.unix.com/u/viki)\
**Post date:** [April 12, 2007, 10:12am UTC](https://community.unix.com/t/parse-xml-file/170482/1 "2007-04-12T10:12:14Z")

</div>

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\>

Further I need to display the data in the following format

b  
data1  
data2  
-----  
d  
data3

Could any body suggest a way to extract the data residing \<a\> \</a\> XML tags.

TIA,  
Viki

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [April 12, 2007, 10:38am UTC](https://community.unix.com/t/parse-xml-file/170482/2 "2007-04-12T10:38:17Z")

</div>

Viki,  
See if this would solve your problem:  
sed -e 's!\</.\>!!' -e 's!\<.\>!!' xml\_file

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [April 13, 2007, 12:46am UTC](https://community.unix.com/t/parse-xml-file/170482/3 "2007-04-13T00:46:16Z")

</div>

```nohighlight
sed -n "/<a>/,/<\/a>/{/<\/*a>/d;s/^<\([^>]*\)>\([^<]*\)<\/\1>/\2/;s/^<\/.*$/--------------/;s/<\(.*\)>/\1/;p;}" file

```

---

<div class="post-metadata">

**Author:** ![viki](https://community.unix.com/letter_avatar/viki/32/6_22734026db12803ebb3e059622ac3534.png) [@viki](https://community.unix.com/u/viki)\
**Post date:** [April 13, 2007, 2:18am UTC](https://community.unix.com/t/parse-xml-file/170482/4 "2007-04-13T02:18:02Z")

</div>

Hi anbu23,

Thanks for quick reply 🙂 . I am getting the following output with the suggested 'sed command'.

\> sed -n "/\<a\>/,/\<\/a\>/{/\<\/\*a\>/d;s/^\<\([^\>]\*\)\>\([^\<]\*\)\<\/\1\>/\2/;s/^\<\/.\*$/--------------/;s/\<\(.\*\)\>/\1/;p;}" c.xml  
b  
c\>data1\</c  
c\>data2\</c  
/b  
d  
c\>data3\</c  
/d

where c.xml contain the following data.

\> cat c.xml  
\<X\>  
.....  
\</X\>  
\<a\>  
\<b\>  
\<c\>data1\</c\>  
\<c\>data2\</c\>  
\</b\>  
\<d\>  
\<c\>data3\</c\>  
\</d\>  
\</a\>

\<XX\>  
...  
\</XX\>

The issue is to extract the XML tags i.e. "b" and "d" and then read the XML tag \<c\>.  
Further store the data in a text file in the following format

b:data1 data2  
d:data3

Could you please help me out.

TIA,  
Viki

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [April 13, 2007, 4:16am UTC](https://community.unix.com/t/parse-xml-file/170482/5 "2007-04-13T04:16:05Z")

</div>

```nohighlight
$ cat file
<X>
.....
</X>
<a>
<b>
<c>data1</c>
<c>data2</c>
</b>
<d>
<c>data3</c>
</d>
</a>

<XX>
...
</XX>
$ sed -n "/<a>/,/<\/a>/{/<\/*a>/d;s/^<\([^>]*\)>\([^<]*\)<\/\1>/\2/;s/^<\/.*$/--------------/;s/<\(.*\)>/\1/;p;}" file
b
data1
data2
--------------
d
data3
--------------

```

I am getting what you have asked.

```nohighlight
$ awk -F"[<>]" ' /<a>/,/<\/a>/ {
> if ( $0 !~ /<\/*a>/ ) {
> if ( $0 == "</" tag ">" ) { print str }
> else if ( NF == 3 ) { str = $2 ":" ; tag=$2 }
> else { str = str " " $3 }
> }
> } ' file
b: data1 data2
d: data3

```

---

<div class="post-metadata">

**Author:** ![viki](https://community.unix.com/letter_avatar/viki/32/6_22734026db12803ebb3e059622ac3534.png) [@viki](https://community.unix.com/u/viki)\
**Post date:** [April 13, 2007, 4:25am UTC](https://community.unix.com/t/parse-xml-file/170482/6 "2007-04-13T04:25:57Z")

</div>

Hi anbu23,

it works.... 🙂

Thanks,  
Viki
