aspose file tools*
The moose likes XML and Related Technologies and the fly likes How do i write a XML file using Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How do i write a XML file using Java?" Watch "How do i write a XML file using Java?" New topic
Author

How do i write a XML file using Java?

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

I have Google and searched a lot but cant seem to find it, basically i would like to learn how do i write an xml file using java,
I have a method that reads a text file and counts the most frequently occuring words
Now i want to write them into xml format and for that im searching for good tutorials aimed at beginner level
for example if the text file has the following;
for-3 now-10 alice-221 wonderland-12
I would then want to write a method that codes the above roughly as;
<frequentlyoccuringwords>
<word occurence>count</word occurence>
<frequentlyoccuringwords>

Do suggest something meaningful


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

You could just output it as text. You know how to print the opening tag, the next tag, the value, the tag after, the value, etc.

Or are you looking for something more complicated?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thank you for the reply but that is not what im looking for.
I want it in proper XML format as referred in my Original post
Koushik Ghosh
Greenhorn

Joined: May 26, 2013
Posts: 25

I'm not sure about your question but I think this code will do your job.


Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thanks Koushik, thats very kind of you. I will give that a try.
However could you refer to me a book too that is like Dietel, plain and simple and targets Java and XML
Koushik Ghosh
Greenhorn

Joined: May 26, 2013
Posts: 25

Sorry I just search in google and see all the solutions available and try to learn from them. I hate books lol :P
In the previous example I have just created a file with .xml extension in C:/ and put the string (which contains count variable) in that file.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

lol, yes i saw the code and also saw what you attempted to do..but honestly that's the bare basics.
What i'm trying to achieve here is a lil bit different.
Let' say there is a file containing the following text
" Humpty dumpty sat on a wall
Humpty fell down
Dumpty laughed aloud"

The java program is supposed to read the above text file and create groups of words occuring multiple times, so it would be as "humpty-02", "dumpty-01", "wall-01" and so forth...
Now i have managed to write the code for the above text classification.
What now i want to do is, something like this ...
<frequentlyoccuring words>
<humpty> 02</humpty>
<dumpty>01</dumpty>
<wall>01</wall>
</frequentlyoccuring words>

This second part of converting it into well formed XML is what i'm stuck and stumped with....
Koushik Ghosh
Greenhorn

Joined: May 26, 2013
Posts: 25

Try this code. I've just put the logic in the method. It will create the xml in C drive.


The output will be the following :
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thanks a ton, Koushik.
That's brilliant.
I wish i could code so promptly, maybe not now but very soon
Till, then; Cheers.
Koushik Ghosh
Greenhorn

Joined: May 26, 2013
Posts: 25

Welcome man...was getting bored at office anyway. cheers..
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

So here is my complete code now, i would still need to tweak your code later so that i can understand it but for now here is the complete code

So my next exercise in this code is to use DocumentBuilderFactory methods of the javax.xml package library because your code uses append method of StringBuffer class .. its fine but i would like to better it by using the xml library package ... so that's the next goal; to achieve. But still, thanks for the idea.
Also there is one flaw in my code, I'm using a predefined string to search for in the ArrayList, but im sure i will figure out how to fix it.
Cheers.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

You can have a look at the support provided by the DOM parser in creating XML files. Or there's another approach by using XMLStreamWriter


Mohamed Sanaulla | My Blog
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Mohamed Sanaulla wrote:You can have a look at the support provided by the DOM parser in creating XML files. Or there's another approach by using XMLStreamWriter

Thanks for the suggestion, however i consider myself to be a begineer when it comes to integrating XML with java.
Could you suggest a good book that tackles this subject at the beginner level?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i write a XML file using Java?