Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Is it possible to insert the "in line DTD code", in the xml file output, created by a java program? (http://www.velocityreviews.com/forums/t500226-is-it-possible-to-insert-the-in-line-dtd-code-in-the-xml-file-output-created-by-a-java-program.html)

Begreen 04-23-2007 03:17 AM

Is it possible to insert the "in line DTD code", in the xml file output, created by a java program?
 
Hi All,


I wrote a java program which outputs a xml file!
But I would prefer this program to insert the DTD code on the fly, in
the xml file when created!


I want the xml file to look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students[
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT student (ssn, firstname)>
<!ELEMENT students (student+)>
]>

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>


BUT not like this below:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">

<students>
<student>
<ssn>444111110</ssn>
<firstname>Jacob</firstname>
</student>
</students>




I used the following code, but it's not working. Doesn't anyone know
what I am doing wrong?

DOMImplementation impl = docBuilderxml.getDOMImplementation();

DocumentType svgDOCTYPE = impl.createDocumentType(
"students", "",
"<!DOCTYPE students [
<!ELEMENT student (ssn, firstname)>
<!ELEMENT ssn (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT students (student+)>]>");

org.w3c.dom.Document doc = impl.createDocument(null, "students",
svgDOCTYPE);


Frank Fredstone 04-23-2007 05:41 AM

Re: Is it possible to insert the "in line DTD code", in the xml file output, created by a java program?
 
Begreen <JenniferCheng99@yahoo.com> writes:

> Hi All,
>
>
> I wrote a java program which outputs a xml file!
> But I would prefer this program to insert the DTD code on the fly, in
> the xml file when created!
>
>
> I want the xml file to look like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!DOCTYPE students[
> <!ELEMENT firstname (#PCDATA)>
> <!ELEMENT ssn (#PCDATA)>
> <!ELEMENT student (ssn, firstname)>
> <!ELEMENT students (student+)>
> ]>
>
> <students>
> <student>
> <ssn>444111110</ssn>
> <firstname>Jacob</firstname>
> </student>
> </students>
>
>
> BUT not like this below:
>
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">
>
> <students>
> <student>
> <ssn>444111110</ssn>
> <firstname>Jacob</firstname>
> </student>
> </students>
>
>
>
>
> I used the following code, but it's not working. Doesn't anyone know
> what I am doing wrong?
>
> DOMImplementation impl = docBuilderxml.getDOMImplementation();
>
> DocumentType svgDOCTYPE = impl.createDocumentType(
> "students", "",
> "<!DOCTYPE students [
> <!ELEMENT student (ssn, firstname)>
> <!ELEMENT ssn (#PCDATA)>
> <!ELEMENT firstname (#PCDATA)>
> <!ELEMENT students (student+)>]>");
>
> org.w3c.dom.Document doc = impl.createDocument(null, "students",
> svgDOCTYPE);


I haven't done what you are trying to do, but I can tell you that
DOMImplementation.createDocumentType() takes a systemId not the text
of a DTD.

The DocumentType object simply refers to a DTD, it doesn't force you
to conform to it when you construct your DOM, so if you just want the
DTD to be able to write it out again, if you have the DTD in a string
anyway, you could output the string when you serialize your DOM, and
skip creating the DocumentType object. For example:

String dtd = "<!DOCTYPE students [ ... ]>";

DOMSource ds = new DOMSource();
Document doc = (Document) ds.getNode();
....
// create your DOM

pw.println("<?xml version='1.0' encoding='UTF-8'?>");
pw.println(dtd);
StreamResult sr = new StreamResult(pw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty("omit-xml-declaration", "true");
trans.transform(ds, sr);


Frank Fredstone 04-23-2007 04:38 PM

Re: Is it possible to insert the "in line DTD code", in the xml file output, created by a java program?
 
Frank Fredstone <none@not.no> writes:

> Begreen <JenniferCheng99@yahoo.com> writes:
>
>> Hi All,
>>
>>
>> I wrote a java program which outputs a xml file!
>> But I would prefer this program to insert the DTD code on the fly, in
>> the xml file when created!
>>
>>
>> I want the xml file to look like this:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>>
>> <!DOCTYPE students[
>> <!ELEMENT firstname (#PCDATA)>
>> <!ELEMENT ssn (#PCDATA)>
>> <!ELEMENT student (ssn, firstname)>
>> <!ELEMENT students (student+)>
>> ]>
>>
>> <students>
>> <student>
>> <ssn>444111110</ssn>
>> <firstname>Jacob</firstname>
>> </student>
>> </students>
>>
>>
>> BUT not like this below:
>>
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>>
>> <!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">
>>
>> <students>
>> <student>
>> <ssn>444111110</ssn>
>> <firstname>Jacob</firstname>
>> </student>
>> </students>
>>
>>
>>
>>
>> I used the following code, but it's not working. Doesn't anyone know
>> what I am doing wrong?
>>
>> DOMImplementation impl = docBuilderxml.getDOMImplementation();
>>
>> DocumentType svgDOCTYPE = impl.createDocumentType(
>> "students", "",
>> "<!DOCTYPE students [
>> <!ELEMENT student (ssn, firstname)>
>> <!ELEMENT ssn (#PCDATA)>
>> <!ELEMENT firstname (#PCDATA)>
>> <!ELEMENT students (student+)>]>");
>>
>> org.w3c.dom.Document doc = impl.createDocument(null, "students",
>> svgDOCTYPE);

>
> I haven't done what you are trying to do, but I can tell you that
> DOMImplementation.createDocumentType() takes a systemId not the text
> of a DTD.
>
> The DocumentType object simply refers to a DTD, it doesn't force you
> to conform to it when you construct your DOM, so if you just want the
> DTD to be able to write it out again, if you have the DTD in a string
> anyway, you could output the string when you serialize your DOM, and
> skip creating the DocumentType object. For example:
>
> String dtd = "<!DOCTYPE students [ ... ]>";
>
> DOMSource ds = new DOMSource();
> Document doc = (Document) ds.getNode();
> ...
> // create your DOM
>
> pw.println("<?xml version='1.0' encoding='UTF-8'?>");
> pw.println(dtd);
> StreamResult sr = new StreamResult(pw);
> TransformerFactory tf = TransformerFactory.newInstance();
> Transformer trans = tf.newTransformer();
> trans.setOutputProperty("omit-xml-declaration", "true");


Oops, also:

trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

> trans.transform(ds, sr);


Franks

Begreen 04-24-2007 02:20 AM

Re: Is it possible to insert the "in line DTD code", in the xml file output, created by a java program?
 
On Apr 22, 10:41 pm, Frank Fredstone <n...@not.no> wrote:
> Begreen <JenniferChen...@yahoo.com> writes:
> > Hi All,

>
> > I wrote a java program which outputs a xml file!
> > But I would prefer this program to insert the DTD code on the fly, in
> > the xml file when created!

>
> > I want the xml file to look like this:

>
> > <?xml version="1.0" encoding="UTF-8"?>

>
> > <!DOCTYPE students[
> > <!ELEMENT firstname (#PCDATA)>
> > <!ELEMENT ssn (#PCDATA)>
> > <!ELEMENT student (ssn, firstname)>
> > <!ELEMENT students (student+)>
> > ]>

>
> > <students>
> > <student>
> > <ssn>444111110</ssn>
> > <firstname>Jacob</firstname>
> > </student>
> > </students>

>
> > BUT not like this below:

>
> > <?xml version="1.0" encoding="UTF-8"?>

>
> > <!DOCTYPE students SYSTEM "StudentsDTDfile.dtd">

>
> > <students>
> > <student>
> > <ssn>444111110</ssn>
> > <firstname>Jacob</firstname>
> > </student>
> > </students>

>
> > I used the following code, but it's not working. Doesn't anyone know
> > what I am doing wrong?

>
> > DOMImplementation impl = docBuilderxml.getDOMImplementation();

>
> > DocumentType svgDOCTYPE = impl.createDocumentType(
> > "students", "",
> > "<!DOCTYPE students [
> > <!ELEMENT student (ssn, firstname)>
> > <!ELEMENT ssn (#PCDATA)>
> > <!ELEMENT firstname (#PCDATA)>
> > <!ELEMENT students (student+)>]>");

>
> > org.w3c.dom.Document doc = impl.createDocument(null, "students",
> > svgDOCTYPE);

>
> I haven't done what you are trying to do, but I can tell you that
> DOMImplementation.createDocumentType() takes a systemId not the text
> of a DTD.
>
> The DocumentType object simply refers to a DTD, it doesn't force you
> to conform to it when you construct your DOM, so if you just want the
> DTD to be able to write it out again, if you have the DTD in a string
> anyway, you could output the string when you serialize your DOM, and
> skip creating the DocumentType object. For example:
>
> String dtd = "<!DOCTYPE students [ ... ]>";
>
> DOMSource ds = new DOMSource();
> Document doc = (Document) ds.getNode();
> ...
> // create your DOM
>
> pw.println("<?xml version='1.0' encoding='UTF-8'?>");
> pw.println(dtd);
> StreamResult sr = new StreamResult(pw);
> TransformerFactory tf = TransformerFactory.newInstance();
> Transformer trans = tf.newTransformer();
> trans.setOutputProperty("omit-xml-declaration", "true");
> trans.transform(ds, sr);- Hide quoted text -
>
> - Show quoted text -


Franks,

Thanks, for the response, but where do you use your doc object?



All times are GMT. The time now is 06:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57