How to create XML Document

How to create XML Document

Post by Brian Matzo » Sat, 23 Sep 2000 04:00:00



Stupid question, but I can't seem to find the answer...:

How do I create a XML Document with a specific DTD?

I can create a "normal" XML document like this:

---
Element root = doc.createElement("Message");
Element item = doc.createElement("From");
item.appendChild(doc.createTextNode("A Person"));
root.appendChild(item);
item = doc.createElement("Body");
item.appendChild(doc.createTextNode("The body"));
root.appendChild(item);
doc.appendChild(root);
---

But I need to add an <!DOCTYPE ...> to it too...
How to do that?

And after it has been created, how do I validate it against that DTD ?

/*
* Cheers
* Brian Matzon
*/

 
 
 

How to create XML Document

Post by locaduc » Sat, 23 Sep 2000 04:00:00


For the same problems I used the followings triks:

Since the level1 of DOM specifications is thought to read and modify
existing XMLs and not to create new ones, the method for creating DOC TYPE
node does not exist.
I resolved the problem by parsing, as first action after Document creation,
a string like this :

<!DOCTYPE MainElement SYSTEM "C:\MyDtd.dtd">
<MainElement />

Then for validate the current DOM Document, I create a new empty documet, I
get the XML from the first one and then load it into the second one. If no
parsing errors occours, the first document is valid.

I hope to have said something right and useful.
Bye
Alessandro <locaduck>


Quote:> Stupid question, but I can't seem to find the answer...:

> How do I create a XML Document with a specific DTD?

> I can create a "normal" XML document like this:

> ---
> Element root = doc.createElement("Message");
> Element item = doc.createElement("From");
> item.appendChild(doc.createTextNode("A Person"));
> root.appendChild(item);
> item = doc.createElement("Body");
> item.appendChild(doc.createTextNode("The body"));
> root.appendChild(item);
> doc.appendChild(root);
> ---

> But I need to add an <!DOCTYPE ...> to it too...
> How to do that?

> And after it has been created, how do I validate it against that DTD ?

> /*
> * Cheers
> * Brian Matzon
> */


 
 
 

How to create XML Document

Post by smilesdot.. » Fri, 20 Oct 2000 04:00:00


Huh?


> For the same problems I used the followings triks:

> Since the level1 of DOM specifications is thought to read and modify
> existing XMLs and not to create new ones, the method for creating DOC
TYPE
> node does not exist.
> I resolved the problem by parsing, as first action after Document
creation,
> a string like this :

> <!DOCTYPE MainElement SYSTEM "C:\MyDtd.dtd">
> <MainElement />

> Then for validate the current DOM Document, I create a new empty
documet, I
> get the XML from the first one and then load it into the second one.
If no
> parsing errors occours, the first document is valid.

> I hope to have said something right and useful.
> Bye
> Alessandro <locaduck>



> > Stupid question, but I can't seem to find the answer...:

> > How do I create a XML Document with a specific DTD?

> > I can create a "normal" XML document like this:

> > ---
> > Element root = doc.createElement("Message");
> > Element item = doc.createElement("From");
> > item.appendChild(doc.createTextNode("A Person"));
> > root.appendChild(item);
> > item = doc.createElement("Body");
> > item.appendChild(doc.createTextNode("The body"));
> > root.appendChild(item);
> > doc.appendChild(root);
> > ---

> > But I need to add an <!DOCTYPE ...> to it too...
> > How to do that?

> > And after it has been created, how do I validate it against that
DTD ?

> > /*
> > * Cheers
> > * Brian Matzon
> > */

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

How to create XML Document

Post by locaduc » Sat, 21 Oct 2000 04:00:00


What's the matter?
Maybe I said a lot of nonsense?

locaduck


> Huh?


> > For the same problems I used the followings triks:

> > Since the level1 of DOM specifications is thought to read and modify
> > existing XMLs and not to create new ones, the method for creating DOC
> TYPE
> > node does not exist.
> > I resolved the problem by parsing, as first action after Document
> creation,
> > a string like this :

> > <!DOCTYPE MainElement SYSTEM "C:\MyDtd.dtd">
> > <MainElement />

> > Then for validate the current DOM Document, I create a new empty
> documet, I
> > get the XML from the first one and then load it into the second one.
> If no
> > parsing errors occours, the first document is valid.

> > I hope to have said something right and useful.
> > Bye
> > Alessandro <locaduck>



> > > Stupid question, but I can't seem to find the answer...:

> > > How do I create a XML Document with a specific DTD?

> > > I can create a "normal" XML document like this:

> > > ---
> > > Element root = doc.createElement("Message");
> > > Element item = doc.createElement("From");
> > > item.appendChild(doc.createTextNode("A Person"));
> > > root.appendChild(item);
> > > item = doc.createElement("Body");
> > > item.appendChild(doc.createTextNode("The body"));
> > > root.appendChild(item);
> > > doc.appendChild(root);
> > > ---

> > > But I need to add an <!DOCTYPE ...> to it too...
> > > How to do that?

> > > And after it has been created, how do I validate it against that
> DTD ?

> > > /*
> > > * Cheers
> > > * Brian Matzon
> > > */

> Sent via Deja.com http://www.deja.com/
> Before you buy.

 
 
 

How to create XML Document

Post by smilesdot.. » Sat, 21 Oct 2000 04:00:00


I need to build a valid XML document in Java.  I have been using JDOM
so far.  How the heck do you do it?  I guess it will involve some kind
of recurive building of sub-trees....

I figured out how to validate an in-memory document:
      SAXBuilder builder2 = new SAXBuilder(true);
      XMLOutputter fmt2 = new XMLOutputter();
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      fmt2.output(validatemeDoc,out);
      ByteArrayInputStream bin = new ByteArrayInputStrea(out.toByteArray
());
      Document validatedDoc = builder2.build(bin);



> What's the matter?
> Maybe I said a lot of nonsense?

> locaduck



> > Huh?


> > > For the same problems I used the followings triks:

> > > Since the level1 of DOM specifications is thought to read and
modify
> > > existing XMLs and not to create new ones, the method for creating
DOC
> > TYPE
> > > node does not exist.
> > > I resolved the problem by parsing, as first action after Document
> > creation,
> > > a string like this :

> > > <!DOCTYPE MainElement SYSTEM "C:\MyDtd.dtd">
> > > <MainElement />

> > > Then for validate the current DOM Document, I create a new empty
> > documet, I
> > > get the XML from the first one and then load it into the second
one.
> > If no
> > > parsing errors occours, the first document is valid.

> > > I hope to have said something right and useful.
> > > Bye
> > > Alessandro <locaduck>



> > > > Stupid question, but I can't seem to find the answer...:

> > > > How do I create a XML Document with a specific DTD?

> > > > I can create a "normal" XML document like this:

> > > > ---
> > > > Element root = doc.createElement("Message");
> > > > Element item = doc.createElement("From");
> > > > item.appendChild(doc.createTextNode("A Person"));
> > > > root.appendChild(item);
> > > > item = doc.createElement("Body");
> > > > item.appendChild(doc.createTextNode("The body"));
> > > > root.appendChild(item);
> > > > doc.appendChild(root);
> > > > ---

> > > > But I need to add an <!DOCTYPE ...> to it too...
> > > > How to do that?

> > > > And after it has been created, how do I validate it against that
> > DTD ?

> > > > /*
> > > > * Cheers
> > > > * Brian Matzon
> > > > */

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.