Zay Ya
Zay Ya

Reputation: 195

How to read recursively xml data from shell script

I'm trying to get xml datas by using shell script code. Current Shell Script Code is @Copy ⇒enter link description here

#!/bin/bash
xmlFile=$1

       function parseXML() {
         elemList=( $(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint   --format - | /bin/grep -e "</.*>$" | while read line; do \
        echo $line | sed -e 's/^.*<\///' | cut -d '>' -f 1; \
      done) )

      totalNoOfTags=${#elemList[@]}; ((totalNoOfTags--))
      suffix=$(echo ${elemList[$totalNoOfTags]} | tr -d '</>')
      suffix="${suffix}_"

      for (( i = 0 ; i < ${#elemList[@]} ; i++ )); do
        elem=${elemList[$i]}
        elemLine=$(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "</$elem>")
        echo $elemLine | grep -e "^</[^ ]*>$" 1>/dev/null 2>&1
        if [ "0" = "$?" ]; then
          continue
        fi
        elemVal=$(echo $elemLine | tr '\011' '\040'| sed -e 's/^[ ]*//' -e 's/^<.*>\([^<].*\)<.*>$/\1/' | sed -e 's/^[ ]*//' | sed -e 's/[ ]*$//')
       echo "$elemVal"  #output
        xmlElem="${suffix}$(echo $elem | sed 's/-/_/g')"
        eval ${xmlElem}=`echo -ne \""${elemVal}"\"`
        attrList=($(cat $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "</$elem>" | tr '\011' '\040' | sed -e 's/^[ ]*//' | cut -d '>' -f 1  | sed -e 's/^<[^ ]*//' | tr "'" '"' | tr '"' '\n'  | tr '=' '\n' | sed -e 's/^[ ]*//' | sed '/^$/d' | tr '\011' '\040' | tr ' ' '>'))
        for (( j = 0 ; j < ${#attrList[@]} ; j++ )); do
          attr=${attrList[$j]}
          ((j++))
          attrVal=$(echo ${attrList[$j]} | tr '>' ' ')
          attrName=`echo -ne ${xmlElem}_${attr}`
          eval ${attrName}=`echo -ne \""${attrVal}"\"`
        done
      done
    }

    parseXML

And, Current XML structure is

 <root>
        <abc>
            <name>zzz</name>
            <email>[email protected]</email>
            <phno>1234589</phno>
        </abc>
        <abc>
            <name>aaaa</name>
            <email>[email protected]</email>
            <phno>2456677</phno>
       </abc>
    </root>

And Current Output is

**

aaaa
[email protected]
2456677
aaaa
[email protected]
2456677

**

but I want to get the ouput structure is

**

zzz
[email protected]
1234589
aaaa
[email protected]
2456677

**

Upvotes: 0

Views: 211

Answers (1)

Cyrus
Cyrus

Reputation: 88686

Use an XML parser like xmllint or xmlstarlet.

xmlstarlet sel -t -v /root/abc file

Output:


            zzz
            [email protected]
            1234589


            aaaa
            [email protected]
            2456677

Upvotes: 1

Related Questions