Help with XML file processing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with XML file processing
# 1  
Old 04-11-2012
Question Help with XML file processing

I need to get all session_ID 's for product="D-0002" from a XML file:

Sample input:
Code:
 <session session_ID="6411206" create_date="2012-04-10-10.22.13.000000">
<marketing_info>
      <program_id>D4AWFU</program_id>
      <subchannel_id>abc</subchannel_id>
    </marketing_info>
    <customer>
      <name>
        <first_name>Darrell</first_name>
        <last_name>Collims</last_name>
      </name>
      <address>
        <first_line></first_line>
        <city></city>
        <state></state>
        <zipcode>75287</zipcode>
      </address>
      <email>dc@gmail.com</email>
    </customer>
    <survey></survey>
    <product_of_interest product="D-0002" year="2013">dart</product_of_interest>
  </session>
 
<session session_ID="6411207" create_date="2012-04-10-10.22.41.000000">
<marketing_info>
      <program_id>D4AWFU</program_id>
      <subchannel_id>def</subchannel_id>
    </marketing_info>
    <customer>
      <name>
        <first_name>Dalton</first_name>
        <last_name>Salyer</last_name>
      </name>
      <address>
        <first_line>po box 4</first_line>
        <city>milford </city>
        <state>OH</state>
        <zipcode>43045</zipcode>
      </address>
      <email>bbq@gmail.com</email>
    </customer>
    <survey></survey>
    <product_of_interest product="D-0002" year="2013">dart</product_of_interest>
  </session>
 
<session session_ID="6411208" create_date="2012-04-10-10.22.47.000000">
<marketing_info>
      <program_id>D4AWFU</program_id>
      <subchannel_id>def</subchannel_id>
    </marketing_info>
    <customer>
      <name>
        <first_name>Darrell</first_name>
        <last_name>Collins</last_name>
      </name>
      <address>
        <first_line></first_line>
        <city></city>
        <state></state>
        <zipcode>75287</zipcode>
      </address>
      <email>dc8@gmail.com</email>
    </customer>
    <survey></survey>
    <product_of_interest product="D-0002" year="2013">dart</product_of_interest>
  </session>
 
<session session_ID="6411210" create_date="2012-04-10-10.23.16.000000">
<marketing_info>
      <program_id>R4AWFU</program_id>
      <subchannel_id>def</subchannel_id>
    </marketing_info>
    <customer>
      <name>
        <first_name>Thomas</first_name>
        <last_name>Hagner</last_name>
      </name>
      <address>
        <first_line>3013</first_line>
        <city>Sturtevant</city>
        <state>WI</state>
        <zipcode>53177</zipcode>
      </address>
      <email>thagr@gmail.com</email>
    </customer>
    <survey></survey>
    <product_of_interest product="R-13-0501" year="2013">1500</product_of_interest>
  </session>
 
<session session_ID="6411212" create_date="2012-04-10-10.23.22.000000">
<marketing_info>
      <program_id>D4AWFU</program_id>
      <subchannel_id>def</subchannel_id>
    </marketing_info>
    <customer>
      <name>
        <first_name>mike</first_name>
        <last_name>conway</last_name>
      </name>
      <address>
        <first_line>6601 e. mcdowell rd</first_line>
        <city>scottsdale</city>
        <state>AZ</state>
        <zipcode>85257</zipcode>
      </address>
      <email>mike@gmail.com</email>
    </customer>
    <survey></survey>
    <product_of_interest product="F-0002" year="2013">dart</product_of_interest>
  </session>

O/p:
Code:
 
6411206
6411207
6411208

Thanks!
# 2  
Old 04-11-2012
With XMLgawk (and, as far as I know, soon with GNU awk too):

Code:
xgawk -lxml 'XMLSTARTELEM == "session" {
  id = XMLATTR["session_ID"]
  }
XMLSTARTELEM == "product_of_interest" && XMLATTR["product"] == "D-0002" {
  print id
  }' infile.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

General Purpose XML Processing

I've been kicking this around for a while now, I might as well post it here. v0.0.9, now properly supporting self-closing tags. v0.0.8, an important quoting fix and a minor change which should handle special <? <!-- etc. tags without seizing up as often. Otherwise the code hasn't changed much.... (6 Replies)
Discussion started by: Corona688
6 Replies

2. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

3. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

4. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

5. Shell Programming and Scripting

processing xml with awk

With the following input sample extracted from a xml file <rel ver="123"> <mod name="on"> <node env="ac" env="1"> <ins ip="10.192.0.1"/> <ins ip="10.192.0.2"/> ... (1 Reply)
Discussion started by: cabrao
1 Replies

6. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

7. Programming

help me with perl script xml processing

Hi everyone, I have Xml files in a folder, I need to extract some attribute values form xml files and store in a hash. My xml file look like this. <?xml version="1.0" encoding="UTF-8"?> <Servicelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"... (0 Replies)
Discussion started by: pavani reddy
0 Replies

8. Shell Programming and Scripting

CSV processing to XML

Hi, i am really fresh with shell scripting and programming, i have an issue i am not able to solve to populate data on my server for Cisco IP phones. I have CSV file within the following format: ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;... (9 Replies)
Discussion started by: angel2008
9 Replies

9. Shell Programming and Scripting

need help on xml processing

I am trying to divide a xml file(my.xml) like this: <?xml version="1.0" encoding="UTF-8"?> <Proto PName="hmmmmmmm"> <Menu id="A" ver="1"> <P> <P name="AA" Type="X"/> <P name="BB" Type="Y"/> <P name="CC" Type="Z"/> </P> ... (4 Replies)
Discussion started by: demoprog
4 Replies

10. Shell Programming and Scripting

Shell Script syntax for XML processing

Hi All, I am new to Shell scripting. I have a log file containing XML Messages.Each XML Message is accompanied with a timestamp.I need to count the the number of messages that get logged in a particular timeinterval.Is there any command/Syntax to achieve this. Any code/example is... (5 Replies)
Discussion started by: vignesh53
5 Replies
Login or Register to Ask a Question