The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 04:59 AM
Need help to parse the file navsharan Shell Programming and Scripting 3 01-17-2008 03:58 PM
Parse XML file viki Shell Programming and Scripting 5 04-13-2007 04:25 AM
Parse file sbasetty Shell Programming and Scripting 5 03-27-2007 01:27 PM
parse text file klick81 Shell Programming and Scripting 3 12-18-2006 12:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-13-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
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 number in a variable, name in a variable, add in a variable

So I could just echo $accountnumb $name $add etc and get the following
65004 Bob Daly Ireland



XML Sample File

<?xml version="1.0"?>




<po:Message
xmlnso="http://192.168.50.167/cust/api"
xmlns="http://192.168.50.167/cust"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://192.168.50.167/cust/api http://database1.com:1008/cust/api/jsp/xsd/api_v3_xsd.jsp"
>
<po:Response>

<po:Header>
<po:RequestUrl><![CDATA[/cust/api/ACCOUNTDetail]]></po:RequestUrl>
<po:RequestCommand>ACCOUNT_DETAILS</po:RequestCommand>
<po:Version>6</po:Version>
<po:Id>000001</po:Id>
<po:Opid><![CDATA[XXX]]></po:Opid>
<po:Status>
<po:Response>SUCCESS</po:Response>
<po:Text>
<![CDATA[Service successfully completed.]]>
</po:Text>
<po:StatusKey><![CDATA[SERVICE_SUCCESS]]></po:StatusKey>
<po:StatusParams cACCOUNT="0">
</po:StatusParams>
</po:Status>
</po:Header>

<po:Body>
<po:RequestKey>
<po:RequestCust>65004</po:RequestCust>
</po:RequestKey>

<ACCOUNT>
<ACCOUNT>65004</ACCOUNT>
<MasterACCOUNT>65004</MasterACCOUNT>
<SubscriberType>SUBS_TYPE_STANDALONE</SubscriberType>
<Name>Bob Daly</Name>
<Add>Ireland</Add>

<ClassDescs>
<ClassDesc><![CDATA[Social Account]]></ClassDesc>
<TempClassDesc><![CDATA[]]></TempClassDesc>
</ClassDescs>
<ClassChangedDateTime></ClassChangedDateTime>

<PreferredLanguage><![CDATA[EN]]></PreferredLanguage>
<PreferredCurrency><![CDATA[EUR]]></PreferredCurrency>
<CurrentPromotionPlan><![CDATA[]]></CurrentPromotionPlan>
<SubscriptionStatus>ACTIVE</SubscriptionStatus>

<TempStatus>NOT_BLOCKED</TempStatus>
<EocnSelStructId>255</EocnSelStructId>

<Agent><![CDATA[00000000]]></Agent>
<SubAgent><![CDATA[]]></SubAgent>
<DisconnectReason><![CDATA[]]></DisconnectReason>

<DisconReasonText><![CDATA[]]></DisconReasonText>

<BeginDate>16-Jul-2008</BeginDate>

<StartDate>16-Jul-2008</StartDate>

<ServiceRemovalDate>12-Sep-2009</ServiceRemovalDate>

<LastModification>12-Sep-2008 13:24:33</LastModification>




</ACCOUNT>
</po:Body>

</po:Response>
</po:Message>

Last edited by frustrated1; 09-13-2008 at 01:52 PM..
  #2 (permalink)  
Old 09-13-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
An example to read those variables in a shell script with awk, adjust it if you want more fields:

Code:
#/bin/sh

awk -F"<|>" '
$2=="ACCOUNT" && NF > 3{s=S3}
$2=="Name"{s=s" "S3}
$2=="Add"{s=s" "$3}
$2=="/ACCOUNT"{print s}
' file | 
while read accountnumb name add; do
  echo "$accountnumb" "$name" "$add"
done
# do something with "$accountnumb" "$name" "$add"
# more commands..
Regards

Last edited by Franklin52; 09-13-2008 at 03:41 PM.. Reason: adjust code
  #3 (permalink)  
Old 09-13-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Thanks for reply - I entered the xml in to a file called file..
Then ran the script you provided but it just returned to command prompt with no output.... Do you know why?
  #4 (permalink)  
Old 09-13-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.
Do you get any output with this?

Code:
#/bin/sh

awk -F"<|>" '
$2=="ACCOUNT" && NF > 3{s=S3}
$2=="Name"{s=s" "S3}
$2=="Add"{s=s" "$3}
$2=="/ACCOUNT"{print s}
' file
  #5 (permalink)  
Old 09-13-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Okay - I now specified to use
/usr/xpg4/bin/awk


I get the following output only when I run the latest script..


Ireland
  #6 (permalink)  
Old 09-13-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
hmmm - I made a small change to the script and now I get

Bob Daly Ireland

I changed from :
$2=="ACCOUNT" && NF > 3{s=S3}
$2=="Name"{s=s" "S3}
$2=="Add"{s=s" "$3}

to:
$2=="ACCOUNT" && NF > 3{s=S3}
$2=="Name"{s=s" "$3}
$2=="Add"{s=s" "$3}


I dont know much about awk to know why this worked and what to do to get the ACCOUNT to work as well... Can you explain what each row of code in the script is actually doing?
  #7 (permalink)  
Old 09-13-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
You could also use CPAN (XML::pick_one): Stepping up from XML::Simple to XML::LibXML
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0