Home > Software design >  Need to fetch value of a key from a XML file using a script
Need to fetch value of a key from a XML file using a script

Time:10-18

I have following XML code where I have to fetch RFCnumber value and assign it to a variable. Ex: From below code , I have to fetch 1200021992 for RFCNumber Property name and assign it to a variable.

<?xml version="1.0" encoding="utf-8"?>
<Objects>
 <Object Type="System.Management.Automation.PSCustomObject">
   <Property Name="d" Type="System.Management.Automation.PSCustomObject">
     <Property Name="__metadata" Type="System.Management.Automation.PSCustomObject">
       <Property Name="id" Type="System.String">https://example.com</Property>
       <Property Name="uri" Type="System.String">https://example.com</Property>
     <Property Name="LongDescription" Type="System.String">Test�Description�of�Change</Property>
     <Property Name="OperationType" Type="System.String">new</Property>
     <Property Name="ChangeType" Type="System.String">ZMCR</Property>
     <Property Name="Status" Type="System.String">Success</Property>
     <Property Name="ShortDescription" Type="System.String">Test�Short�Description</Property>
     <Property Name="CycleTypeId" Type="System.String">8200000083</Property>
     <Property Name="RfcNumber" Type="System.String">1200021992</Property>

I need some help in this. I'm trying following code but it is not working:

$rfc = ($response -split "<d:RfcNumber>|</d:RfcNumber>")[X]
echo $rfc

CodePudding user response:

1st solution: With your shown samples please try following awk code.

awk -F'[><]' 
'/<Property Name="RfcNumber" Type="System\.String">/{
   print $3
}
' Input_file

OR if you have only 1 RefNumber and you want to print only 1 value then better exit from command to save sometime in that case you can try following awk code a bit tweaked form of above:

awk -F'[><]' 
'/<Property Name="RfcNumber" Type="System\.String">/{
   print $3
   exit
}
' Input_file

Explanation: Simple explanation would be, setting -F(field separator) as > and < and in main block of program checking if line contains <Property Name="RfcNumber" Type="System.String" then print 3rd field which is required output by OP.



2nd solution: Using sed you can try following solution once.

sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p}' Input_file

OR in case you have only 1 match of the value in whole file then better to quit/exit from program rather than reading whole Input_file in that case try following:

sed -n '/<Property Name="RfcNumber" Type="System\.String"/{s/.*">//;s/<\/.*//p;q}' Input_file

CodePudding user response:

Using sed

$ rfc=$(sed -En '/RfcNumber/s/[^>]*>([^<]*).*/\1/p' input_file)
$ echo "$rfc"
1200021992

CodePudding user response:

Why not use the built-in xml parser in powershell? e.g.:

[xml] $x = gc infile.xml
$x.objects.object.property | ? name -like "rfcnumber" | select -exp "#text"

Output:

1200021992

CodePudding user response:

As it is xml, using for exampe xmllint with an xpath query:

xmllint --xpath '//Property[@Name="RfcNumber"]/text()' file.xml

Output

1200021992
  • Related