How to convert a generic xml format block to properties format String from an xml file using ksh script

With the conditions:

  1. I cannot use any XML parser tool as I don't have permission , read only

  2. My xmllint version does not support xpath, and I cannot update it , read only

  3. I dont have xmlstarlet and cannot install it

My options are limited to almost String processing.

An input parameter will be provided by the user and it will be the name of the block.

So,

let's assume

NAME=${USER_INPUT}

and we will use $NAME as the parameter to search the xml block that will be searched.

How can I convert a generic xml block like this in an xml file like this:

<block>
 <name>Bob</name>
 <address>USA</address>
 <email>bob@dogs.com</email>
 <phone>1234567</phone>
</block>

<block>
 <name>Peter</name>
 <food>France</address>
 <cell>peter@pizza.com</cell>
 <drinks>Coke</drinks>
 <car>Honda</car>
 <bike>Mountain bike</bike>
</block>

So the thing that I want to achieve here is to get the xml block that satisfies my search ,

for example NAME=Bob ;

The output of the script should be in properties file format

name=Bob
address=USA
email=bob@dogs.com
phone=1234567

A thing to consider here is that the xml format per block are different. The nodes are not the same for every xml block.

Answers


With bash and GNU sed:

#!/bin/bash

NAME="Bob"
sed -n '/name>'${NAME}'/,/<\/block>/s/.*<\(.*\)>\(.*\)<.*/\1=\2/p' file.xml

Output:

name=Bob
address=USA
email=bob@dogs.com
phone=1234567

Need Your Help

Android UI layout component resizing wrongly on font size change

android xml android-layout layout

I have a layout with 3 components that align perfectly as long as the user doesn't change the font size in display settings on their android phones...

Why is jsPDF outputing only blank pages on larger documents?

javascript jquery canvas jspdf html2canvas

I have a function that calls html2canvas. If the canvas is only 1 pdf 'page' large it works no problem. If it is larger than that I made this while statement to break up the canvas into smaller can...