Closing XML tags in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Closing XML tags in one line
# 8  
Old 03-14-2012
Thanks dears, here is the file example and below is the result I got with your commands
Code:
<?xml version="1.0" encoding="UTF-8"?>
<identifier>1234567890</identifier>

<tag>
<ntype>multi</ntype>
<mobileSubscriberType>genericSubscriber</mobileSubscriberType>
<umtsSubscriber>
<accTypeGSM>true</accTypeGSM>
<accTypeGERAN>true</accTypeGERAN>
<accTypeUTRAN>true</accTypeUTRAN>
</umtsSubscriber>
<wllSubscriber>false</wllSubscriber>
<mscat>10</mscat>
<odboc>0</odboc>
<odbic>0</odbic>
<odbr>0</odbr>
<odboprc>0</odboprc>
<odbssm>0</odbssm>
<clip>true</clip>
<clipOverride>false</clipOverride>
<colpOverride>false</colpOverride>
<hold>true</hold>
<mpty>true</mpty>
<nwa>3</nwa>
<odbgprs>0</odbgprs>
<rr>WhiteL</rr>
<sr>2</sr>
<odbsci>0</odbsci>
<ts11>
<msisdn>12345</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>12345</msisdn>
</ts21>
<ts22>
<msisdn>12345</msisdn>
</ts22>
<bs30genr>
<msisdn>12345</msisdn>
<bcieID>VIDEO</bcieID>
</bs30genr>
<gprs>
<msisdn>12345</msisdn>
</gprs>
<cfu>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>
<cfu>
<basicServiceGroup>BS30-dataSync</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>
<cfb>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<isdnNumber>1234567</isdnNumber>
<status>7</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
<notifyForwardingSubscriber>false</notifyForwardingSubscriber>
<ftnoType>internat</ftnoType>
</cfb>
</tag>



Result which was fine for some lines but not for others
Code:
#awk '/^<\/.*>$/{print $0",";next}{printf $0","}END{printf "\n"}' testtag
<?xml version="1.0" encoding="UTF-8"?>,<identifier>1234567890</identifier>,,<tag>,<ntype>multi</ntype>,<mobileSubscriberType>genericSubscriber</mobileSubscriberType>,<umtsSubscriber>,<accTypeGSM>true</accTypeGSM>,<accTypeGERAN>true</accTypeGERAN>,<accTypeUTRAN>true</accTypeUTRAN>,</umtsSubscriber>,
<wllSubscriber>false</wllSubscriber>,<mscat>10</mscat>,<odboc>0</odboc>,<odbic>0</odbic>,<odbr>0</odbr>,<odboprc>0</odboprc>,<odbssm>0</odbssm>,<clip>true</clip>,<clipOverride>false</clipOverride>,<colpOverride>false</colpOverride>,<hold>true</hold>,<mpty>true</mpty>,<nwa>3</nwa>,<odbgprs>0</odbgprs>,<rr>WhiteL</rr>,<sr>2</sr>,<odbsci>0</odbsci>,<ts11>,<msisdn>12345</msisdn>,<bcieID>TELEPHON</bcieID>,</ts11>,
<ts21>,<msisdn>12345</msisdn>,</ts21>,
<ts22>,<msisdn>12345</msisdn>,</ts22>,
<bs30genr>,<msisdn>12345</msisdn>,<bcieID>VIDEO</bcieID>,</bs30genr>,
<gprs>,<msisdn>12345</msisdn>,</gprs>,
<cfu>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>,
<cfu>,<basicServiceGroup>BS30-dataSync</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>,
<cfb>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<isdnNumber>1234567</isdnNumber>,<status>7</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,<notifyForwardingSubscriber>false</notifyForwardingSubscriber>,<ftnoType>internat</ftnoType>,</cfb>,


Last edited by Corona688; 03-14-2012 at 06:42 PM..
# 9  
Old 03-14-2012
First, please use code tags: video tutorial on using code tags. Second, post desired output for this sample data.
# 10  
Old 03-14-2012
It is best to use a XML tool to process XML file.

Install Perl-XML-XPath

Code:
# yum install perl-XML-XPath.noarch
or
# perl -MCPAN -e 'install XML::XPath'

Try this:
Code:
$ cat a.xml
<?xml version="1.0"?>
<something>
<ts11>
<msisdn>123</msisdn>
<bcieID>TELEPHON</bcieID>
</ts11>
<ts21>
<msisdn>987</msisdn>
</ts21>
<cfu>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<status>4</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
</cfu>

<cfb>
<basicServiceGroup>TS10-telephony</basicServiceGroup>
<isdnNumber>12345</isdnNumber>
<status>7</status>
<notifyCallingSubscriber>false</notifyCallingSubscriber>
<notifyForwardingSubscriber>false</notifyForwardingSubscriber>
<ftnoType>internat</ftnoType>
</cfb>
</something>


$ cat a.pl
#! /usr/bin/perl
use XML::XPath;

$xp=XML::XPath->new($ARGV[0]);
$nodes=$xp->find('/something/*');
foreach $n ($nodes->get_nodelist) {
        print  $n->getName(), "\n";
        $str=$n->toString();
        $str=~s/\n//g;
        $str=~s/></>,</g;
        print $str, "\n";
}


$ ./a.pl a.xml
ts11
<ts11>,<msisdn>123</msisdn>,<bcieID>TELEPHON</bcieID>,</ts11>
ts21
<ts21>,<msisdn>987</msisdn>,</ts21>
cfu
<cfu>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<status>4</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,</cfu>
cfb
<cfb>,<basicServiceGroup>TS10-telephony</basicServiceGroup>,<isdnNumber>12345</isdnNumber>,<status>7</status>,<notifyCallingSubscriber>false</notifyCallingSubscriber>,<notifyForwardingSubscriber>false</notifyForwardingSubscriber>,<ftnoType>internat</ftnoType>,</cfb>

# 11  
Old 03-18-2012
Thanks a lot dears, with Perl it works perfectly...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

Cutting all xml tags out of a line

I would like search and find a word (easily identified by 'key') from an xml file and then cut all of the tags out of the resulting line (anything between a < and a >) and display the remaining material. I am running Debian and mksh shell. dictionary.sh: #!/bin/sh key='key="'$1'"><form'... (3 Replies)
Discussion started by: bedtime
3 Replies

3. Shell Programming and Scripting

How to insert new line in xml?

I have an xml like below and wanted to insert a new line between end of the tag and start of the new tag. <partyaddress><addressline1>gandhi stree</addressline1><city>hyderbad</city><state>AP</state><addressline1>xyz stree</addressline1><city>bangalore</city><state>Karnataka</state>... (6 Replies)
Discussion started by: D_Sethi
6 Replies

4. Shell Programming and Scripting

Print a closing XML tag shell script

I have a shell script that does everything I need it to do. But, when I was testing it I realized it doesn't print the closing XML tag.... Does anyone know how to incorporate printing the XML tag with my script? I am using AWK any help would be appreciated. (4 Replies)
Discussion started by: risarose87
4 Replies

5. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

6. Shell Programming and Scripting

Compare two xml files while ignoring some xml tags

I've got two different files and want to compare them. File 1 : <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record id="38,557"><columns><column><name>orge... (2 Replies)
Discussion started by: Shaishav Shah
2 Replies

7. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

8. Shell Programming and Scripting

Insert a new line between the XML tags?.

<TestLog> <TriggerAPI> <StartDate>Nov 16, 2012 6:34:02 AM com.satttest01.Response() </StartDate> <RequestType>SUCCESS: Send :</RequestType> <TranNumber>5210203</TranNumber> <TranId>8585319731207148</TranId> </TriggerAPI> <TriggerAPI> <StartDate>Nov 16, 2012 6:34:02 AM... (3 Replies)
Discussion started by: laknar
3 Replies

9. 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

10. Shell Programming and Scripting

Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs. What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text... (4 Replies)
Discussion started by: DeuceLee
4 Replies
Login or Register to Ask a Question