Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Begreen - 23 Apr 2007 04:17 GMT
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 - 23 Apr 2007 06:41 GMT
> Hi All,
>
[quoted text clipped - 48 lines]
> 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 - 23 Apr 2007 17:38 GMT
>> Hi All,
>>
[quoted text clipped - 72 lines]
> Transformer trans = tf.newTransformer();
> trans.setOutputProperty("omit-xml-declaration", "true");

Oops, also:

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

> trans.transform(ds, sr);

Franks
Begreen - 24 Apr 2007 03:20 GMT
> > Hi All,
>
[quoted text clipped - 75 lines]
>
> - Show quoted text -

Franks,

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.