![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 xmlns o="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.. |
|
||||
|
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..
Last edited by Franklin52; 09-13-2008 at 03:41 PM.. Reason: adjust code |
|
||||
|
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? |
|
||||
|
You could also use CPAN (XML::pick_one): Stepping up from XML::Simple to XML::LibXML
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|