Parse XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse XML
# 1  
Old 01-16-2011
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:

Code:
<?xml version="1.0" encoding="UTF-8" ?> 
<queue> 
    <paused>True</paused> 
    <kbpersec>0.0</kbpersec> 
    <mbleft>854.697840691</mbleft> 
    <mb>854.697840691</mb> 
    <noofslots>1</noofslots> 
    <diskspace1>21.980682373</diskspace1> 
    <diskspace2>21.980682373</diskspace2> 
    <timeleft>0:00:00</timeleft>     
    <jobs>
        <job> 
        <id>SABnzbd_nzo_zt2syz</id> 
        <msgid>3066202</msgid> 
        <filename>Ubuntu 8.04 (Hardy Heron) - Desktop CD x64</filename> 
        <mbleft>854.697840691</mbleft> 
        <mb>854.697840691</mb> 
        <index>0</index> 
        <unpackopts>3</unpackopts> 
        <cat>Apps</cat> 
        <script>snarl.exe</script> 
        <status>Queued</status> 
        <percentage>0</percentage> 
        <bytes>896215643.0</bytes> 
        </job>
    </jobs>
</queue>

Any help getting the XML parsed would be great! Thanks in advance!
# 2  
Old 01-16-2011
You'd better use an XML parser, shell is not good at it.
# 3  
Old 01-16-2011
Quote:
Originally Posted by kevintse
You'd better use an XML parser, shell is not good at it.
Thanks. I just realized that shell probably isn't going to be very powerful for this. Looks like Perl would be a better option. Anyone have any examples that would work with my XML example?
# 4  
Old 01-16-2011
it depends on what language you are comfortable with. I like ruby , so here's a ruby example
Code:
  #!/usr/bin/env ruby  -Ku 
require 'rexml/document' 
include REXML 
File.open("file") do |doc| 
  xml = Document.new(doc) 
  xml.elements.each("queue") do |elem| 
    elem.elements.each {|x| print "->#{x}\n" } 
  end 
end

of course, other languages like Perl/Python etc have XML libraries as well. Read their documentation for usage.
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

Parse xml file

I am trying to create a shell script that will parse an xml file (file attached). awk '/Id v=/ { print }' Test.xml | sed 's!<Id v=\"\(.*\)\"/>!\1!' > output.txt An output.txt file is created but it is empty. It should contain the value 222159 in it. Thanks. (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Parse XML using xmllint

Hi All, Need help to parse the xml file in shell script using xmllint. Below is the sample xml file. <CARS> <AUDI> <Speed="45"/> <speed="55"/> <speed="75"/> <speed="95"/> </AUDI> <BMW> <Speed="30"/> <speed="75"/> <speed="120"/> <speed="135"/> </BMW>... (6 Replies)
Discussion started by: prasanna2166
6 Replies

4. Shell Programming and Scripting

Parse XML File.

HI Guys I have Below XML File : <xn:SubNetwork id="XYZ"> <xn:SubNetwork id="C01"> <xn:MeContext id="CO1"> <xn:ManagedElement id="1"> <un:RncFunction id="1"> <un:UtranCell id="NY431"> ... (2 Replies)
Discussion started by: pareshkp
2 Replies

5. UNIX for Dummies Questions & Answers

Parse xml file

HI Guys, Input .XML <xn:MeContext id="L0307"> <xn:ManagedElement id="1"> <xn:VsDataContainer id="1"> <xn:attributes> <xn:vsDataType>vsDataENodeBFunction</xn:vsDataType> ... (3 Replies)
Discussion started by: pareshkp
3 Replies

6. Shell Programming and Scripting

Parse XML line

Hi I am having an xml file with lines like these <d name="T2tt_350_100" title="T2tt_012j_350_100_428p4_pPF_PU" add="1" color="4" ls="1" lw="2" normf="1" xsection="0.070152" EqLumi="94651.6"... (2 Replies)
Discussion started by: Alkass
2 Replies

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

8. 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

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