I have a XML file of little huge size. I have been provided with that and all I need to do is a extract some values
in between the XML tags. Since I don't have the XML
parser utility available in my machines. I am looking for an alternate method.
To start with, there is a XML tag <capacity> </capacity
> which repeats n number of time in the XML file and in between this XML tags there are many other different tags as well.
I have to get each occurrence of <capacity> </capacity>
XML tag separately and then parse through that and extract the values under them.
<subcolumns><capacity><name>45.90</name>
<index>0</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>57.09</name>
<index>1</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
<capacity><name>55</name>
<index>2</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
</subcolumns>
So the logic which I thought was to find the first occurrence of a <capacity> </capacity>
XML tag and print it to a temp file and then delete that first occurrence.
<capacity><name>45.90</name>
<index>0</index>
<value_type>String</value_type>
<ignore_case_flag>1</ignore_case_flag>
<hidden_flag>0</hidden_flag>
<exclude_from_parse_flag>1</exclude_from_parse_flag>
</capacity>
Henceforth when this is done for the second time the new pair of <capacity> </capacity>
XML tag is taken into consideration.
So this has to repeat for multiple times until the last <capacity> </capacity>
tag is found. And each time this part is extracted the data will be changing and that can be extracted.
Now all I want is to select the first occurrence of <capacity> </capacity>
XML tag from the master XML file & print it to temp file and delete that part.
And this is what I tried and nothing worked for me.
sed -n '2,${/<capacity>\(.*\)<\/capacity>/\1/p;q;}' "<input XML file>" >> temp.txt
My further idea is to take that temp file for processing and extract the values which I need to under the capacity
tags. For which I have already written the logic and it is working fine.
<capacity>
and</capacity>
always be on separate lines or can they appear on the same line? Can you have nested tags? I mean one<capacity> </capacity>
inside another<capacity> </capacity>
? Is there any specific reason why you are doing it this way instead of using an XML parser?xml
parser. However, it isn't possible to answer your question in that context, since you didn't disclose what data you want to extract and in what format.