Need help in extracting data from xml file

Hello,

This is my first post in here, so excuse me if I sound too noob here!

I need to extract the path "/apps/mp/installedApps/V61/HRO/hrms_01698_A_qa.ear" from the below xml extract. The path will always appear with the key "binariesURL"

<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1273611400395" deploymentId="0" startingWeight="1" binariesURL="/apps/mp/installedApps/V61/HRO/hrms_01698_A_qa.ear" useMetadataFromBinaries="true" enableDistribution="true" createMBeansForResources="true" reloadInterval="60" reloadEnabled="true" appContextIDForSecurity="href:CellV61_HRO/hrms_01698_A_qa" filePermission=".*\.dll=755#.*\.so=755#.*\.a=755#.*\.sl=755" allowDispatchRemoteInclude="false" allowServiceRemoteInclude="false">

The whole of the above XML text appears in a single line. And I am trying to do this through a bash shell script.

Any help will be highly appreciated.

 perl -nle 'print $1 if /binariesURL=\"(.+?)\"\s+/' inputfile.xml

OR

awk '{for(i=1;i<=NF;i++) { if ($i ~ /binariesURL/){split($i,a,"\"");print a[2]}}}' input.xml
1 Like

I also tried this just now and working:

cat file.xml | awk '{ print$6 }' | awk -F"\"" '{ print$2}'

sed 's/ /\n/g' inputfile | sed -n 's/"//g;s/binariesURL=//p'

No need to cat the file. That is a UUOC! The following eliminates the UUOC.

awk '{ print$6 }' file.xml | awk -F"\"" '{ print$2} 

You can also simply this to:

$ awk -F'[ "]' '{ print $15 }' file.xml
/apps/mp/installedApps/V61/HRO/hrms_01698_A_qa.ear

If you need more generic solution/ideas to convert XML to CSV or into other format = easier to use in scripts, then maybe my XML to CSV howto give more ideas.

-jukka-

I always hint on this, that using a XML DOM/SAX parser is the right way to parse an XML file