![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 4
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2004
Posts: 4
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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.datYou 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." |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2004
Posts: 4
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|