Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 27th, 2004, 2:58 PM   #1
leoman
Newbie
 
Join Date: Sep 2004
Posts: 4
Rep Power: 0 leoman is on a distinguished road
I have a xml file in which there is data from multiple tables.
The file looks like --
test.xml
<code>
<Activities>
<ACTIVITY_ID>2</ACTIVITY_ID>
<CREATED_DT>2004-08-24T10:46:53.917</CREATED_DT>
<CREATED_BY>X</CREATED_DT>
</Activities>
<Activities>
<ACTIVITY_ID>3</ACTIVITY_ID>
<CREATED_DT>2004-08-24T10:46:53.917</CREATED_DT>
<CREATED_BY>X</CREATED_DT>
</Activities>
<Contact>
<Name>V1</Name>
<Phone>513-333-3333</Phone>
</Contact>
<Contact>
<Name>V2</Name>
<Phone>513-555-4444</Phone>
</Contact>
<Contact>
<Name>V3</Name>
<Phone>513-666-6666</Phone>
</Contact>
</code>
I want to make two file from this
Activities.xml and second Contact.xml

Activities.xml will have
<code>
<Activities>
<ACTIVITY_ID>2</ACTIVITY_ID>
<CREATED_DT>2004-08-24T10:46:53.917</CREATED_DT>
<CREATED_BY>X</CREATED_DT>
</Activities>
<Activities>
<ACTIVITY_ID>3</ACTIVITY_ID>
<CREATED_DT>2004-08-24T10:46:53.917</CREATED_DT>
<CREATED_BY>X</CREATED_DT>
</Activities>
</code>
And Contact.xml will have
<code>
<Contact>
<Name>V1</Name>
<Phone>513-333-3333</Phone>
</Contact>
<Contact>
<Name>V2</Name>
<Phone>513-555-4444</Phone>
</Contact>
<Contact>
<Name>V3</Name>
<Phone>513-666-6666</Phone>
</Contact>
</code>
The number of rows in each table will vary. Also there are more than two tables , so tags like Activity and Contact can be many, each one having many rows.
I need to create sepearte file for each of these main tags.

Please help
Thanks!
Leo
leoman is offline   Reply With Quote
Old Sep 27th, 2004, 4:10 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Read the file content in line by line, searching for each opening tag... compare that tag to the ones you want to isolate. For instance, if the activies tag is open, create a new file send each line that you read in, into the new file until the closing activity tag is reached, then save that file and begin looking for the next opening tag's section / Contacts.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 27th, 2004, 4:13 PM   #3
leoman
Newbie
 
Join Date: Sep 2004
Posts: 4
Rep Power: 0 leoman is on a distinguished road
Thanks for your reply.

Can you give me a small code snippet for this.
I am a newbiew for unix shell scripts.

Thanks once again
leoman is offline   Reply With Quote
Old Sep 27th, 2004, 4:27 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
For starters... here is something you can build off of... this bit of code reads in a file (named myfile.dat) and outputs each line to the screen.

#!/bin/sh

while read f
do
    echo $f
done < myfile.dat


You can compare $f to <Activities> and the other tags you need to determine what you want to do. You can easily redirect $f to a new file if you find a match for the entire content of the <Activities> section... until </Activities> is reached by using pipes, etc.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 28th, 2004, 8:42 AM   #5
leoman
Newbie
 
Join Date: Sep 2004
Posts: 4
Rep Power: 0 leoman is on a distinguished road
Thanks for providing me with sample code.
I could create a smaple file with this example.

But I have a small problem here, sinch the file that we will be provided form other system will not have new line charcater after each tag.
When I open in any xml editor this file is seen the way I posted in my thred above. But if I open the file in vi editor or word it is not having any line feeds between the tags.

Can you think of any solution for this?

Thanks a lot for sharing your valuable input !

Leo
leoman is offline   Reply With Quote
Old Sep 28th, 2004, 2:01 PM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
If you have more on each info on each line, one way you could do it is have an awk function in your shell script that parses each line and extracts the attribute... for comparison in the 'workload' section of your script.

For instance:

After a line is read in from the data file... send it to the awk script.
The awk script looks for <, if it finds it... it reads the name of the attribute until the ending tag >. This gathers 'Activies' into a variable used for comparison in which you generate the new file until you find the closing attribute tag </Activies>.... etc. Read file, get line into variable, parse the line variable for the XML attribute tags.. generate new file where needed, etc.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC