Convert tag based lines to xml format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert tag based lines to xml format
# 1  
Old 05-05-2014
Convert tag based lines to xml format

Hi All,

Can some one help me to convert this line of code to xml format.

Thanks in advance,
preethy.

input:

Code:
Response><OverAllStatus>Success</OverAllStatus><OverAllDescription>COMMON_SUCCESS</OverAllDescription><ResponseElement><status>Success</status><Description>COMMON_SUCCESS</Description><Handle_Type>MalcRaptorXP_Device</Handle_Type><Handle_Id>534</Handle_Id><Handle_Group>99999</Handle_Group><Root_Type>Root</Root_Type><Device_Id>534</Device_Id><Device_Group>99999</Device_Group><Region_Group>99999</Region_Group><Device_Type>MalcRaptorXP_Device</Device_Type><Root_Group>0</Root_Group><Root_Id>0</Root_Id><Region_Type>Region</Region_Type><Region_Id>14</Region_Id><name>WLLK-MALCXP3 (459th & HWY42)</name></ResponseElement><ResponseElement><status>Success</status><Description>COMMON_SUCCESS</Description><Handle_Type>Mx23_Device</Handle_Type><Handle_Id>685</Handle_Id><Handle_Group>99999</Handle_Group><Root_Type>Root</Root_Type><Device_Id>685</Device_Id><Device_Group>99999</Device_Group><Region_Group>99999</Region_Group><Device_Type>Mx23_Device</Device_Type><Root_Group>0</Root_Group><Root_Id>0</Root_Id><Region_Type>Region</Region_Type><Region_Id>17</Region_Id><name>CSTR-MXK1</name></ResponseElement></Response>

required output:

Code:
Response><OverAllStatus>Success</OverAllStatus><OverAllDescription>COMMON_SUCCESS</OverAllDescription>
<ResponseElement>
	<status>Success</status>
	<Description>COMMON_SUCCESS</Description>
	<Handle_Type>MalcRaptorXP_Device</Handle_Type>
	<Handle_Id>534</Handle_Id>
	<Handle_Group>99999</Handle_Group>
	<Root_Type>Root</Root_Type>
	.
	.
	.
</ResponseElement>
<ResponseElement>
	<status>Success</status>
	<Description>COMMON_SUCCESS</Description>
	<Handle_Type>Mx23_Device</Handle_Type>
	.
	.
	.
</ResponseElement>
</Response>

# 2  
Old 05-05-2014
You can use utils like xmllint tp format xml strings. Check if its available for you.

Code:
echo '<Response><OverAllStatus>....</Response>' | xmllint --format -

Code:
<Response>
  <OverAllStatus>Success</OverAllStatus>
  <OverAllDescription>COMMON_SUCCESS</OverAllDescription>
  <ResponseElement>
    <status>Success</status>
    <Description>COMMON_SUCCESS</Description>
    <Handle_Type>MalcRaptorXP_Device</Handle_Type>
    <Handle_Id>534</Handle_Id>
    <Handle_Group>99999</Handle_Group>
    <Root_Type>Root</Root_Type>
    <Device_Id>534</Device_Id>
    <Device_Group>99999</Device_Group>
    <Region_Group>99999</Region_Group>
    <Device_Type>MalcRaptorXP_Device</Device_Type>
    <Root_Group>0</Root_Group>
    <Root_Id>0</Root_Id>
    <Region_Type>Region</Region_Type>
    <Region_Id>14</Region_Id>
    <name>WLLK-MALCXP3 (459th _ HWY42)</name>
  </ResponseElement>
  <ResponseElement>
    <status>Success</status>
    <Description>COMMON_SUCCESS</Description>
    <Handle_Type>Mx23_Device</Handle_Type>
    <Handle_Id>685</Handle_Id>
    <Handle_Group>99999</Handle_Group>
    <Root_Type>Root</Root_Type>
    <Device_Id>685</Device_Id>
    <Device_Group>99999</Device_Group>
    <Region_Group>99999</Region_Group>
    <Device_Type>Mx23_Device</Device_Type>
    <Root_Group>0</Root_Group>
    <Root_Id>0</Root_Id>
    <Region_Type>Region</Region_Type>
    <Region_Id>17</Region_Id>
    <name>CSTR-MXK1</name>
  </ResponseElement>
</Response>

for me, the & in your string causing issues for me in parsing. I didn't try the alternatives.
Also please note you missed < in the beginning
# 3  
Old 05-05-2014
replace 'sample.xml' with your file
Code:
awk '{gsub("<ResponseElement>|</ResponseElement>|</Response>", "\n&")}1' sample.xml | \
  awk '{if($0 ~ /^<ResponseElement>/)
    {gsub("<[^>]*><", "&\n\t<");
	gsub("><\n", ">\n")}}1'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. Shell Programming and Scripting

Help with tag value extraction from xml file based on a matching condition

Hi , I have a situation where I need to search an xml file for the presence of a tag <FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then extract the value from the following tag <_LocalId> which is "CW2094139". There... (2 Replies)
Discussion started by: paul1234
2 Replies

3. Shell Programming and Scripting

Help with XML tag value extraction based on condition

sample xml file part <?xml version="1.0" encoding="UTF-8"?><ContractWorkspace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _LoadId="export_AJ6iAFmh+pQHq1" xsi:noNamespaceSchemaLocation="ContractWorkspace.xsd"> <_LocalId>CW2218471</_LocalId> <Active>true</Active> ... (3 Replies)
Discussion started by: paul1234
3 Replies

4. Shell Programming and Scripting

Help with XML tag value extraction based on matching condition

sample xml file part <DocumentMinorVersion>0</DocumentMinorVersion> <DocumentVersion>1</DocumentVersion> <EffectiveDate>2017-05-30T00:00:00Z</EffectiveDate> <FollowOnFrom> <ContractRequest _LoadId="export_AJ6iAFoh6g0rE9"> <_LocalId>CRW2218451</_LocalId> ... (4 Replies)
Discussion started by: paul1234
4 Replies

5. Shell Programming and Scripting

Creating multiple xml tag lines in a file

Hi All, Can someone tell me how can we create same xml tag lines based on the number of lines present in other file and replace the Name variable vaule present in other file. basically I have this xml line <typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/> and say I... (4 Replies)
Discussion started by: Optimus81
4 Replies

6. Shell Programming and Scripting

How can I remove some xml tag lines using shell script?

Hi All, My name is Prathyu and I am working as a ETL develper. I have one requirement to create a XML file based on the provided XSD file. As per the Datastage standards Key(repeatable) field does not contain any Null values so I am inserting some dummy tag line to that XML file. ... (14 Replies)
Discussion started by: Prathyu
14 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

Extract multiple xml tag value into CSV format

Hi All, Need your assistance on another xml tag related issue. I have a xml file as below: <INVOICES> <INVOICE> <BILL> <BILL_NO>1234</BILL_NO> <BILL_DATE>01 JAN 2011</BILL_DATE> </BILL> <NAMEINFO> <NAME>ABC</NAME> </NAMEINFO> </INVOICE> <INVOICE> <BILL> <BILL_NO>5678</BILL_NO>... (12 Replies)
Discussion started by: angshuman
12 Replies

9. Shell Programming and Scripting

How to remove some xml tag lines using shell script

I have existing XML file as below, now based on input string in shell script on workordercode i need to create a seprate xml file for e.g if we pass the input string as 184851 then it find the tag data from <workOrder>..</workOrder> and write to a new file and similarly next time if i pass the... (3 Replies)
Discussion started by: balrajg
3 Replies

10. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies
Login or Register to Ask a Question