XML format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML format
# 1  
Old 06-30-2010
XML format

Please help to convert the file which is tab separated into xml format
Code:
'Date'  'ID'    'Name'  'Time'  'Average Time'
2010-06-02      3       30943   00:00:50        1.39
2010-06-02      1       4545    00:01:03
2010-06-02      9       8203    00:01:00

# 2  
Old 06-30-2010
With Perl and XML::Simple:

Code:
perl -F\\t -MXML::Simple -lane'
  @keys = @F and next if $. == 1;
  %hash = ();
  $hash{$keys[$_]} = [$F[$_]] for 0..$#F;
  print XMLout(\%hash, RootName => "whatever");
  ' infile

Which produces:
Code:
zsh-4.3.10[t]% perl -F\\t -MXML::Simple -lane'
quote>   @keys = @F and next if $. == 1;
quote>   %hash = ();
quote>   $hash{$keys[$_]} = [$F[$_]] for 0..$#F;
quote>   print XMLout(\%hash, RootName => "whatever");
quote>   ' infile
<whatever>
  <'Average Time'>1.39</'Average Time'>
  <'Date'>2010-06-02</'Date'>
  <'ID'>3</'ID'>
  <'Name'>30943</'Name'>
  <'Time'>00:00:50</'Time'>
</whatever>

<whatever>
  <'Date'>2010-06-02</'Date'>
  <'ID'>1</'ID'>
  <'Name'>4545</'Name'>
  <'Time'>00:01:03</'Time'>
</whatever>

<whatever>
  <'Date'>2010-06-02</'Date'>
  <'ID'>9</'ID'>
  <'Name'>8203</'Name'>
  <'Time'>00:01:00</'Time'>
</whatever>

# 3  
Old 06-30-2010
Good ol'e shell script:

Code:
#!/bin/ksh93
#!/bin/bash
L=label
{
read line
eval H=($line)
while read line
do
  eval P=($line)
  printf "<$L>\n"
  for ((i=0;i<${#P[@]};i++))
  do
    printf "  %s\n" "<${H[i]}>${P[i]}</${H[i]}>"
  done
  printf "</$L>\n"
done
} < infile

Code:
<label>
  <Date>2010-06-02</Date>
  <ID>3</ID>
  <Name>30943</Name>
  <Time>00:00:50</Time>
  <Average Time>1.39</Average Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>1</ID>
  <Name>4545</Name>
  <Time>00:01:03</Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>9</ID>
  <Name>8203</Name>
  <Time>00:01:00</Time>
</label>

# 4  
Old 06-30-2010
Humm, As far as XML is concerned I think that the AverageTime element always needs to be included even if it is empty i.e.
Code:
<label>
  <Date>2010-06-02</Date>
  <ID>3</ID>
  <Name>30943</Name>
  <Time>00:00:50</Time>
  <Average Time>1.39</Average Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>1</ID>
  <Name>4545</Name>
  <Time>00:01:03</Time>
  <Average Time></Average Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>9</ID>
  <Name>8203</Name>
  <Time>00:01:00</Time>
  <Average Time></Average Time>
</label>



---------- Post updated at 02:08 PM ---------- Previous update was at 12:22 PM ----------

Here is another way of converting the CSV to XML using python
Code:
import csv

csv.register_dialect('custom', delimiter='\t', skipinitialspace=True)

ifile = file("file.csv")
data = csv.reader(ifile, dialect='custom')
tags = data.next()
l = len(tags);

with ifile:
    data = csv.reader(ifile, dialect='custom')
    print "<labels>"
    for record in data:
         print "  <label>"
         for i, field in enumerate(record):
             print "     <"+tags[i].strip('\'')+">"+field+"</"+tags[i].strip('\'')+">"
         i += 1
         while (i < len(tags)):
             print "     <"+tags[i].strip('\'')+"></"+tags[i].strip('\'')+">"
             i += 1
         print "  </label>"
    print "</labels>"

This outputs
Code:
<labels>
  <label>
     <Date>2010-06-02</Date>
     <ID>3</ID>
     <Name>30943</Name>
     <Time>00:00:50</Time>
     <Average Time>1.39</Average Time>
  </label>
  <label>
     <Date>2010-06-02</Date>
     <ID>1</ID>
     <Name>4545</Name>
     <Time>00:01:03</Time>
     <Average Time></Average Time>
  </label>
  <label>
     <Date>2010-06-02</Date>
     <ID>9</ID>
     <Name>8203</Name>
     <Time>00:01:00</Time>
     <Average Time></Average Time>
  </label>
</labels>

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 06-30-2010
To include empty average time fields:

Code:
#!/bin/ksh93
#!/bin/bash
L=label
{
read line
eval H=($line)
while read line
do
  eval P=($line)
  printf "<$L>\n"
  for ((i=0;i<${#H[@]};i++))
  do
    printf "  %s\n" "<${H[i]}>${P[i]}</${H[i]}>"
  done
  printf "</$L>\n"
done
} < infile

Output:
Code:
<label>
  <Date>2010-06-02</Date>
  <ID>3</ID>
  <Name>30943</Name>
  <Time>00:00:50</Time>
  <Average Time>1.39</Average Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>1</ID>
  <Name>4545</Name>
  <Time>00:01:03</Time>
  <Average Time></Average Time>
</label>
<label>
  <Date>2010-06-02</Date>
  <ID>9</ID>
  <Name>8203</Name>
  <Time>00:01:00</Time>
  <Average Time></Average Time>
</label>

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: ... (2 Replies)
Discussion started by: preethy
2 Replies

2. Shell Programming and Scripting

How can I replace data in between xml tags to required format?

Hi All, I have a requirement to change the data in xml file to required format. Below is the scenario. Please find the attached Xml file which contains data that I need to convert.. <ReleaseIndicatorList><ReleaseIndicator>Alternative... (0 Replies)
Discussion started by: Prathyu
0 Replies

3. Shell Programming and Scripting

From sql Insert Query to XML format

Hi How do I translate Let say Cat inserts.sql gives Insert into PM9_TAXATION_ROUNDING (STATE_GECODE, TAX_TYPE, TAX_AUTHORITY, SYS_CREATION_DATE, SYS_UPDATE_DATE, APPLICATION_ID, DL_SERVICE_CODE, ROUNDING_METHOD) Values ('xx', 'xx', 'x', TO_DATE('10/26/2012 13:01:20',... (3 Replies)
Discussion started by: anuj87in
3 Replies

4. Shell Programming and Scripting

XML dateTime format in shell script

How can i frame current date & time in the xml standard dateTime format in shell script 2011-10-18T12:00:00.000000 I got up to the date format using the below code date '+%Y'-'%m'-'%d'T ---------- Post updated at 09:10 AM ---------- Previous update was at 08:53 AM ---------- ... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

Need to split a xml file in proper format

Hi, I have a file which has xml data but all in single line Ex - <?xml version="1.0"?><User><Name>Robert</Name><Location>California</Location><Occupation>Programmer</Occupation></User> I want to split the data in proper xml format Ex- <?xml version="1.0"?> <User> <Name>Robert</Name>... (6 Replies)
Discussion started by: avishek007
6 Replies

6. UNIX for Dummies Questions & Answers

XML / XSD: what is a good format for amounts?

Suppose I have a XSD definition of amount: <xs:element name="Amount" type="amount> And the amounts themselves are given in the following format in the XML: <amount>1.000.00</amount> In other words, the thousand separator and the decimal point are the same. Does this require that the parser... (3 Replies)
Discussion started by: figaro
3 Replies

7. Shell Programming and Scripting

how to parse the file in xml format using awk/nawk

Hi All, I have an xml file with the below format. <a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f> output needed is 111,222,333,123,234 111,222,333,456,789 nawk 'BEGIN{FS="<|>"} {print a,b,c,e,f a="" ... (7 Replies)
Discussion started by: natalie23
7 Replies

8. Shell Programming and Scripting

Convert XML to CSV format

Can any one give the idea on this, please. I have the following XML file and wants to convert into CSV(comma separated value) format. <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE Waveset PUBLIC 'waveset.dtd' 'waveset.dtd'> <Waveset> <Object name='ra8736'> <Attribute name='ADDRESS'... (2 Replies)
Discussion started by: kumar04
2 Replies

9. Shell Programming and Scripting

format xml

i want to grep to a file the numerator-denominator, 1.23456, 1.78901 from the following xml tags. <locks:pair xc:value="numerator/denominator" xc:type="Fields"> <mp:formFactor>1</mp:formFactor> <mp:quotation>numerator-denominator</mp:quotation> <mp:one>1.23456</mp:one>... (5 Replies)
Discussion started by: sigh2010
5 Replies
Login or Register to Ask a Question