how to build xml documents

D

Dom

Hi to everyone,
I'm developing a java application that:
A) read some values from a database
B) build an XML file according to a specific DTD

Is there any open source library/component allowing to create an XML
document (file), like that:

doc.startdocument();
doc.startelement("A");
doc.startelement("B");
doc.startelement("C");
doc.addValue("cccccc");
doc.endelement("C");
doc.startelement("D");
doc.addValue("dddddd");
doc.endelement("D");
doc.endelement("B");
doc.endelement("A");
doc.endDocument();

to generate this file:
<A>
<B>
<C> cccccc </C>
<D> ddddd </D>
</B>
</A>

thanks
bye dom
 
O

Oliver Wong

Dom said:
Hi to everyone,
I'm developing a java application that:
A) read some values from a database
B) build an XML file according to a specific DTD
Is there any open source library/component allowing to create an XML
document (file), like that:

We use Castor (http://www.castor.org/) to accomplish this. We've been
using it with XSDs, but there may be support for DTDs as well (haven't
checked). If not, it's open source, so you can add in support for DTDs, or
you can get a DTD to XSD converter.

- Oliver
 
F

franco

A) database persistance is something you might want to look into, check
out hybernate.

B) try xmlbeans, in summary it takes a schema and compiles a package of
java classes that are bound to the schema definition, and methods that
allow serialization and parsing of xml text.

C) [your example] this looks like DOM.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder factory.newDocumentBuilder();

Document doc = builder.newDocument;

Element a = doc.createElement("A");
doc.appendChild(a);

Element b = doc.createElement("B");
a.appendChild(b);

Element c = doc.createElement("C");
c.appendChild(doc.createText("cccccc"));
b.appendChild(c);

Element d = doc.createElement("D");
d.appendChild(doc.createText("dddddd"));
b.appendChild(d);

is an example in DOM howto generate what you want.
 
D

Dom

Thanks very much to all.

I think I wille be applying solution 3 because:
A) data are derived from different datasources (file, databases, ...)
B) I haven't DTD or XSD

bye
 
R

Roedy Green

doc.startdocument();
doc.startelement("A");
doc.startelement("B");
doc.startelement("C");
doc.addValue("cccccc");
doc.endelement("C");
doc.startelement("D");
doc.addValue("dddddd");
doc.endelement("D");
doc.endelement("B");
doc.endelement("A");

those methods are utterly trivial. If you don't want syntax checking
for balance, and other restrictions, you could implement them each
with a println statement. You then can avoid downloading an XML
library.
 
F

franco

you should really look into using a document definition of some sort,
no matter which way you build xml, it should be valid when its done
being built
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top