• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do i write a XML file using Java?

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 42074
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure about your question but I think this code will do your job.


 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome man...was getting bored at office anyway. cheers..
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Did you just should on me? You should read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic