File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Making multiple files from single file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Making multiple files from single file" Watch "Making multiple files from single file" New topic
Author

Making multiple files from single file

Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
I have a xml file in which there is data from multiple tables.
The file looks like --
test.xml

I want to make two file from this
Activities.xml and second Contact.xml

Activities.xml will have

And Contact.xml will have

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.

Is it possible to do it using shell script? Or should I be using something else?

Please help
Thanks!

[ EJFH: Fixed up code samples. ]
[ September 27, 2004: Message edited by: Ernest Friedman-Hill ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

These files don't look like xml at all.
Are you sure, that that's the real content of the files, and not only the rendered output of some tool?

If the format is allways

(int)
(date)
X

or:

V(int)
no3-no3-no4

a script, reading the file and evaluating regex should work.
Do you use bash?

I guess awk is the best tool to do the work, but I'm not well in awk.
Here is a sed-solution, which will eliminate the blanks:


Note that I inserted 3 CRs into the statements, which shall avoid an endless line here in the forum, which you have to remove for the script to work.
And for the time, the pattern could be made more restrictive.
[ September 27, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

His files were indeed XML, but this forum is one of the few at the Ranch which allows HTML tags in messages, so all his XML elements were being ignored as unknown HTML tags! I fixed them all up.


[Jess in Action][AskingGoodQuestions]
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Thanks Ernest for fixing the files!

Thanks Stefan for your input.

I do not use bash. Infact I am very much a newbie with unix shell scripts.
If you could give code / code snippet for doing this that will be very helpful.

THanks once again!

Gemini
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

Actually, the best solution is probably to use XSLT. I beleive that the program in question is "xslide", but I can't bet to my Linux box to check.

Of course, whether it's more painful to built XSLT templates or simply hack and slash your way through raw text using AWK or Perl (hint: check CPAN for XML support packages) is debatable.


Customer surveys are for companies who didn't pay proper attention to begin with.
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Ernest Friedman-Hill , Can you please move this thread to XML forum?

If someone can give me some heads up with how I can write XSLT for this.

I can use Oracle Pl SQL or Java Stored procedures in my project here.

Is it possible to do this using Plsql ? how efficient will that be?

Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I can certainly move this to XML.

The XSLT would be pretty simple -- I'll see if I have time to come back later and talk about it.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

He he - teaching the xml-people a little bit linux-bash:


replace

with


like to see a simpler solution using xml-libs.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
One of the advantages of using XSLT is that you are guaranteed to get a valid XML as input (consequently in this example as output), which your example is not.

<CREATED_BY>X</CREATED_DT>
should probably be

<CREATED_BY>X</CREATED_BY>?

And then, you missed the top level element.

After fixing this, your XSLT 1.0 solution will be


replace match="Activities" with "Contacts" for Contact.xml.


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
I am confused about this one still.

Here are further details.
The file that I have is a huge file with data from many tables.
Basically I have to load this xml file into Oracle tables.
I have written Oracle Pl sql procedure using XML_Save pacakge to achive loading of data given, file name (file should have data only from one table), and table name to which the data should be loaded.

Now the problem I am facing is, the file that our system will receive will have data from all the tables together. Not that I will get seperate file for each table. Henc I thought of breaking the files into various files such that each file will have data from only one table.

I tried to do it using Unix shell scripts. But have not been successful yet. Reason being --
The file that our system is receiving will all this data in single line (with no newline character at all) (Isn't it crazy???)
Also the file size is around 5MB.

Any suggestions as to what should I use to break this file.
The contets of the files will vary (Sometime Activity table will not have any data in it to no rows with <Activities> tag will be there.
Hence if I understand correctly both he solutions suggested above are not able to handle this dynamic nature of the file.

I have tried using sed in Unix but it did not work due to I guess file size and basically this dumb file having no newline characters in it.


Please help!

Thanks

[ October 01, 2004: Message edited by: Gemini Moses ]
[ October 01, 2004: Message edited by: Gemini Moses ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making multiple files from single file
 
Similar Threads
printing out all my instances...toString() method..
So exhausted...anyone there to help?
Reference Specific Line in Text File
Problem reading data from file, putting it into map, then writing it back to text doc.
Java dunce is back...still can't figure this out