Java XML document creation

A

alfienorks

I'm new to XML document creation with Java and I've got to admit I'm
struggling.

I need to generate an XML file structure in Java (no content - just the
structure) based on an array of parameters as follows:

XMLNodesArray[] = { '/a/b/c', '/a/b/d', '/z/y/x', '/z/y/a' };

and what I need is an (string) output of:

<root>
<a>
<b>
<c></c>
<d></d>
</b>
</a>
<z>
<y>
<x></x>
<a></a>
</y>
</z>
</root>

Firstly is there a quick way of converting these slash delimited tokens
into XML
Alternatively is there a Java-XML-Document-Creation-101 guide anywhere
that might tell me how to navigate a growing XML tree and insert
missing tokens as I trawl through the XMLNodesArray?

Any thoughts - or guidance on XML document generation coding?

many thanks,
Al
 
J

jan V

Any thoughts - or guidance on XML document generation coding?
many thanks,

There are (literally) stacks of books on XML, and there are almighty tangles
of XML info on the web. I'd say, start reading...
 
A

alfienorks

Jan V - I've looked at your other "helpful-comments" you've posted in
the past ... if you haven't got anything useful to say then please
don't say anything - most of your postings say "read a book" - well why
don't you do that (do you happen to work for a publisher by any chance)
and stay off the web as YOU ARE JUST NOT HELPING.
 
J

jan V

Jan V - I've looked at your other "helpful-comments" you've posted in
the past ... if you haven't got anything useful to say then please
don't say anything - most of your postings say "read a book" - well why
don't you do that (do you happen to work for a publisher by any chance)
and stay off the web as YOU ARE JUST NOT HELPING.

Clearly I'm not helping you, ... that's not to say that many others could
not react with "A book, for goodness sake, of course, I'll get myself a book
on this and become an expert on the subject."

I regard reading well-written technical books as a professional necessity,
and I regard anyone claiming to be part of our profession who refuses to
read same as thoroughly outside the profession. Your opinion on this issue
may of course differ.
 
R

Roedy Green

XMLNodesArray[] = { '/a/b/c', '/a/b/d', '/z/y/x', '/z/y/a' };

this notation so far as I know is something you cooked up, and empty
data files are not something there is a big demand for. So I will
hazard a guess you wont' find a canned solution.

Tackling the problem from scratch, here is how I would proceed.

1. use regex to convert a/b/c to new String[]{ "a", "b", "c" };

2. write some looping code to convert that to
<root>
<a>
<b>
<c></c>
</b>
</a>
</root>

3. write some looping code to convert two lines '/a/b/c', '/a/b/d' to
what you want.

4. keep going, adding more lines until you see the general patterns.
 
T

Thomas Weidenfeller

Jan V - I've looked at your other "helpful-comments" you've posted in
the past ... if you haven't got anything useful to say then please
don't say anything - most of your postings say "read a book" - well why
don't you do that (do you happen to work for a publisher by any chance)
and stay off the web as YOU ARE JUST NOT HELPING.

This is not a help desk. This is a discussion group. If you need
predictable help consider paying someone. Whining will not help either.

/Thomas
 
A

alfienorks

Thanks Roedy, but I think I've got the solution (the problem I had was
virtually nobody talked about creating XML - they only focused on
reading it) - anyway I'm not sure if I should simply send everyone to a
book to read up on (which works if we all have access to that book - or
shall I just post the solution so that anyone reading the web at the
moment can see). What do you think Jan - shall I help - or shall I just
be painful and suggest anyone needing a quick solution goes to the
library for a couple of days?

[This next part is not directed to Roedy] This is a discussion group
with people with real world problems looking for help, pointers,
suggestions and code examples - not responses from people that have
nothing better to do than lord over newsgroups in the vain belief that
they know best without actually helping and all they are doing is
cluttering up the web with their useless verbiage. ToW/JaV (if you're
not the same person) - please continue to clutter up this thread with
things nobody wants to read (probably suggesting we read a book on what
website to surf on another lonely Saturday night) as I will not be
watching it now.
Perhaps if Jan can look/go away then I'll post the code,




private String test(Vector slashtags) throws Exception {

String output = "";

Element tree = new Element("root");
Element elements = style;
String ftp = "";
String list = "";
String tag = "";

Enumeration enum = slashtags.elements();
while (enum.hasMoreElements()) {
slashList = (String) enum.nextElement();
StringTokenizer st = new StringTokenizer(slashList, "/");

elements = style;
while (st.hasMoreElements()) {
tag = (String) st.nextElement();
List list = elements.getChildren();
Element tagElement = new Element(tag);

boolean found = false;
for (int i= 0; i< list.size(); i++) {
if (((Element)
list.get(i)).getName().equals(tagElement)) {
found = true;
}
}
if (!found) {
elements.addContent(newElement);
}

elements = elements.getChild(tagElement);
}

}

// Return an XML looking string based on the Element tree...
return getEString(tree);
}


private String getEString(Element tree) throws Exception {

String result = "";
XMLOutputter serializer = new XMLOutputter();
result = serializer.outputString(tree);
return result;
}
 
A

Andrew Thompson

[This next part is not directed to Roedy] This is a discussion group
with people with real world problems looking for help, pointers,
suggestions and code examples - not responses from people that have
nothing better to do than lord over newsgroups in the vain belief that
they know best without actually helping and all they are doing is
cluttering up the web with their useless verbiage. ToW/JaV (if you're
not the same person) - please continue to clutter up this thread with
things nobody wants to read (probably suggesting we read a book on what
website to surf on another lonely Saturday night) as I will not be
watching it now.
Perhaps if Jan can look/go away then I'll post the code,

Perhaps you should grow up, instead.

jan V gets straight to the point. If you don't like that,
maybe you need to find a 'nice forum'[1].

As an aside, those who know of the many helpful postings[2]
by jan V would recognise that your complaints are merely the
rantings of a tempramental, self centered, immature person
who mistakes this forum for a help desk.

[2] A sample of recent posts..
<http://groups.google.com/group/comp.lang.java.programmer/msg/64978aea76c9d892>
<http://groups.google.com/group/comp.lang.java.programmer/msg/08c06d0f2a6374cc>
<http://groups.google.com/group/comp.lang.java.programmer/msg/2ab521f20822d1ad>
<http://groups.google.com/group/comp.lang.java.programmer/msg/739aa5761a4f728b>

[1] <http://www.physci.org/codes/javafaq.jsp#cljh>
 
J

jan V

moment can see). What do you think Jan - shall I help - or shall I just
be painful and suggest anyone needing a quick solution goes to the
library for a couple of days?

My dear fellow, you have a strange perspective on showing gratitude.

When you come to any newsgroup asking questions, then all those who respond
to your post with any productive advice whatsoever (whether it's advice you
agree with, or not) are trying to help *you*.

In any culture, helping your fellow man is called being generous, and in any
culture, generosity received by a polite individual deserves the expression
of gratitude. What you're doing is biting the hand that feeds you, so like
Andrew correctly points out, that's gross immaturity.

Now, if you don't mind, I fancy helping a few other people stay on the rails
of good software engineering while using Java as an implementation language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top