XML to Table conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML to Table conversion
# 1  
Old 08-06-2015
XML to Table conversion

<title>00000888</title>

<name>XYZ</name>

<updated>2397-2323</updated>

<content>Content</content>

<summary>summary12</summary>

<title>00000788</title>

<name>ABC</name>

<updated>99090-09</updated>

<content>Content123</content>

<summary>summary12adf</summary>

I want to convert the above format to a table format like below

title name updated content summary
00000888 XYZ 2397-2323 Content123 summary12adf

Could some one help me with it , I already tried with awk but no luck.
# 2  
Old 08-06-2015
Hello Nanduri,

Could you please use code tags for commands/codes/inputs you are showing in your codes as per forum rules. Could you please try following and let me know if this helps you.
Code:
 awk -F"[><]" '{B=B?B OFS $2:$2;A=A?A OFS $3:$3} END{print B ORS A}'  Input_file

Output will be as follows.
Code:
 title  name  updated  content  summary  title  name  updated  content  summary
00000888  XYZ  2397-2323  Content  summary12  00000788  ABC  99090-09  Content123  summary12adf

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-06-2015
Thanks for the reply ,

But in my XML file the content is more than a single word , Its taking each word in a separate field.
Code:
<title>00000888 asdf asdf </title>

<name>XYZ adsf asdf </name>

<updated>2397-2323 adf adsf </updated>

<content>Content asdf adsf </content>

<summary>summary12 asdf asdf </summary>

Code:
Title ; Name;  Updated ; Content; Summary 
00000888 asdf asdf ; XYZ adsf asdf ; 2397-2323 adf adsf; Content asdf adsf ; summary12 asdf asdf 
000788 asdf asdf ; XYS asdf sadf ; 8912-1234 asd asd ; Content asdf adf ; summary 16 asdf asdf

# 4  
Old 08-06-2015
Hello nanduri,

Following may help you in same.
Code:
 awk -F"[><]" '{sub(/\</,X,$2);if($2=="title"){i++;};B[i]=NR?B[i] OFS $2:$2;$2=$NF="";gsub(/[[:space:]]+$|[[:space:]]+|\/.*/,X,$0);A[i]=A[i]?A[i] " asdf asdf ;" OFS $0:$0} END{print B[1];for(o=1;o<=i;o++){;print A[o]}}' Input_file

EDIT: Adding non-one liner form of solution for same.
Code:
 awk -F"[><]" '{
                sub(/\</,X,$2);
                if($2=="title") {
                                        i++;
                                };
                B[i]=NR?B[i] OFS $2:$2;
                $2=$NF="";gsub(/[[:space:]]+$|[[:space:]]+|\/.*/,X,$0);
                A[i]=A[i]?A[i] " asdf asdf ;" OFS $0:$0
              }
                END{
                                        print B[1];
                                        for(o=1;o<=i;o++){;
                                                                print A[o]
                                                         }
                   }
              ' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-06-2015 at 10:25 AM.. Reason: Added a non-one liner solution now.
# 5  
Old 08-07-2015
Well, I don't think those "asdf" should be taken literally. For any space separated text as the respective field contents, try
Code:
awk -v HD="title name updated content summary" '
BEGIN           {for (N=i=split (HD, T); i>0; i--) SRCH[T[i]]
                 for (i=1; i<=N; i++) printf "%s\t", T[i]
                 print ""
                }
                {TMP=$0
                 gsub (/<|>.*$/, "", TMP)
                }
L &&
TMP == T[1]     {for (i=1; i<=N; i++) printf "%s\t", PR[T[i]]
                 print ""
                 delete PR
                }

TMP in SRCH     {gsub (/<[^>]*>/, "")
                 PR[TMP]=$0
                 L=1
                }
END             {for (i=1; i<=N; i++) printf "%s\t", PR[T[i]]
                 print ""
                }

' file

---------- Post updated at 11:30 ---------- Previous update was at 11:29 ----------

Unfortunately you changed horses (OFS) between your post#1 and #3; adapt the script accordingly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Getting Rid of Annoying Bootstrap Table Borders and Wayward Table Lines

Bootstrap is great; but we have had some issues with Bootstrapped <tables> (and legacy <fieldset> elements) showing annoying, wayward lines. I solved that problem today with this simple jQuery in the footer: <script> $(function(){ $('tr, td, fieldset,... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Script to load XML file to Oracle table

Experts. I have created a oracle table as below. create table xml_tab ( File_No number , File_content Varchar2(2000), file_type xmltype ); Daily we are receiving many XML files as below. here is our sample xml file. File1 : (7 Replies)
Discussion started by: vasuvv
7 Replies

3. UNIX for Beginners Questions & Answers

Replacing string/special characters using a 'conversion' table

Hi, Does anyone know if there is a script or program available out there that uses a conversion table to replace special characters from a file? I am trying to remove some special characters from a file but there are several unprintable/control characters that some I need to remove but some I... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Build a table from a list by comparing existing table entries

I am new to this shell scripting.... I have a file which contains list of users. This files get updated when new user comes into the system. I want to create script which will give a table containing unique list of users. When I say unique, it means script should match table while parsing... (3 Replies)
Discussion started by: dchavan1901
3 Replies

5. UNIX for Dummies Questions & Answers

Find & Replace identifiers using a conversion table

Hi ! I have input.tab with one column containing Item IDs under a number format (the second column is the Location of this item): Location Item ID rack1 12; 35; 43 rack35 23; 894; 5478; 98 etc... (The number of Items per row is variable. Item IDs in a same field are... (17 Replies)
Discussion started by: lucasvs
17 Replies

6. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

7. Shell Programming and Scripting

Text to XML conversion using script

Hi I have a file in the format with so many records DB2 Universal JDBC Driver Provider,wdialdcsq,New JDBC Datasource,jdbc/wdialdcsq,,dcsqdb2n,cldrdgw1.is.chrysler.com,2998,DB2,10,1,180,0,1800 i need to convert all these into <DataSource name="wdialODDC" maxConnection="10" minConnection="0 "... (1 Reply)
Discussion started by: mskalyani9
1 Replies

8. Shell Programming and Scripting

How to export table data to xml file?

Hi , I would like to get some suggestion from the experts. My requirement is to export oracle table data as an xml file. Any unix/linux tools, scripts available? Regards, (2 Replies)
Discussion started by: LinuxLearner
2 Replies

9. Shell Programming and Scripting

select values from db1 table and insert into table of DB2

Hi I am having three oracle databases running in three different machine. their ip address is different. from one of the DB am able to access both the databases.(means am able to select values and insert values in to tables individually.) I need to fetch some data from DB1 table(say DB1 ip is... (2 Replies)
Discussion started by: aemunathan
2 Replies

10. UNIX for Advanced & Expert Users

About XML file Integrating to a table

Hi , I have a question about unix shell scripting. I hope someone may help me to solve problem. In our system , there are two server that generate two different log file which are XML format in UNIX system. The problem is , this XML files contain such counter names and values sometimes this... (1 Reply)
Discussion started by: kbiricik
1 Replies
Login or Register to Ask a Question