DOCTYPE not created

B

bill turner

I've searched the web and news groups. It seems others have had this
problem. Unfortunately, the solution seems to be exactly what I've
coded. If anybody can point out what is wrong, I'd truly appreciate
it. What I am trying to do is to write an xml file to a local disk
file. Everything else comes out just fine. However, the DOCTYPE node
is not created. I've tried a number of different things, including
specifying that I wanted a validating implementation, all to no avail.
Here is a snippet of the relative code:

<snippet>
DocumentBuilderFactory builderFactory
= DocumentBuilderFactory.newInstance();
builderFactory.setValidating(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
//DocumentType docType =impl.createDocumentType("trapbatch",
"","trapmonkey.dtd");
DocumentType docType = impl.createDocumentType("trapbatch",null,
"trapmonkey.dtd");

//doc = impl.createDocument("", "trapbatch", docType);
doc = impl.createDocument(null, "trapbatch", docType);

rootElement = doc.getDocumentElement();
doc.insertBefore(doc.createComment("a good rule of thumb is to use
child elements rather than attributes if the information feels like
data"), rootElement);
rootElement.appendChild(doc.createComment("enterpriseSpecfic Traps"));
rootElement.appendChild(doc.createComment("if repeat attribute = 0,
the the delayInMillis attribute is meaningless. The The delayInMillis
attribute is defaulted by the application in all other situations"));

for (int i = 0; i < this.size(); i++)
{
trap = this.get(i);

trapElement = doc.createElement("trap");
trapElement.setAttribute("repeat", trap.getRepeat());

trapElement.setAttribute("delayInMillis",trap.getDelayInMilliseconds());

rootElement.appendChild(trapElement);
 
R

Richard

Bill,

After all the nodes have been added, try this:

String doctype = "-//Bigwebsite//doctype" ;
String dtdname = "http://Bigwebsite/dtdname" ;

javax.xml.transform.Source source = new
javax.xml.transform.dom.DOMSource(doc) ;
Transformer transformer =
TransformerFactory.newInstance().newTransformer() ;
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,doctype);
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,dtdname);

File file = new File("outfile.xml") ;
Result sr2 = new javax.xml.transform.stream.StreamResult(file);
transformer.transform(source, sr2) ;

=========================
 
R

Richard

Bill,

After all the nodes have been added, try this:

String doctype = "-//Bigwebsite//doctype" ;
String dtdname = "http://Bigwebsite/dtdname" ;

javax.xml.transform.Source source = new
javax.xml.transform.dom.DOMSource(doc) ;
Transformer transformer =
TransformerFactory.newInstance().newTransformer() ;
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,doctype);
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,dtdname);

File file = new File("outfile.xml") ;
Result sr2 = new javax.xml.transform.stream.StreamResult(file);
transformer.transform(source, sr2) ;

=========================
 
P

Philippe Poulard

bill said:
I've searched the web and news groups. It seems others have had this
problem. Unfortunately, the solution seems to be exactly what I've
coded. If anybody can point out what is wrong, I'd truly appreciate
it. What I am trying to do is to write an xml file to a local disk
file. Everything else comes out just fine. However, the DOCTYPE node
is not created. I've tried a number of different things, including
specifying that I wanted a validating implementation, all to no avail.
Here is a snippet of the relative code:

<snippet>
DocumentBuilderFactory builderFactory
= DocumentBuilderFactory.newInstance();
builderFactory.setValidating(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
//DocumentType docType =impl.createDocumentType("trapbatch",
"","trapmonkey.dtd");
DocumentType docType = impl.createDocumentType("trapbatch",null,
"trapmonkey.dtd");

//doc = impl.createDocument("", "trapbatch", docType);
doc = impl.createDocument(null, "trapbatch", docType);

rootElement = doc.getDocumentElement();
doc.insertBefore(doc.createComment("a good rule of thumb is to use
child elements rather than attributes if the information feels like
data"), rootElement);
rootElement.appendChild(doc.createComment("enterpriseSpecfic Traps"));
rootElement.appendChild(doc.createComment("if repeat attribute = 0,
the the delayInMillis attribute is meaningless. The The delayInMillis
attribute is defaulted by the application in all other situations"));

for (int i = 0; i < this.size(); i++)
{
trap = this.get(i);

trapElement = doc.createElement("trap");
trapElement.setAttribute("repeat", trap.getRepeat());

trapElement.setAttribute("delayInMillis",trap.getDelayInMilliseconds());

rootElement.appendChild(trapElement);
.
.
.
}

TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

hi,

the problem comes from the serializer : the default transformer just
perform a copy ; in xslt, the public and system ids must be set
explicitely :

Transformer transformer = tFactory.newTransformer( myStylesheet );

myStylesheet must be a stylesheet that performs a copy AND indicates the
output format :

<xsl:eek:utput
doctype-public = string
doctype-system = string
/>

an other solution is to use another serializer instead of xslt's
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new
File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
TrapBatch.TRAP_TEMPLATE_TEST_FILE));
transformer.transform(source, result);


</snippet>

Again, thanks for your help!

Bill


--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
P

Philippe Poulard

Philippe said:
hi,

the problem comes from the serializer : the default transformer just
perform a copy ; in xslt, the public and system ids must be set
explicitely :

Transformer transformer = tFactory.newTransformer( myStylesheet );

myStylesheet must be a stylesheet that performs a copy AND indicates the
output format :

<xsl:eek:utput
doctype-public = string
doctype-system = string
/>

an other solution is to use another serializer instead of xslt's

there is a more convenient way : JAXP provides a mean to set the public
and system id directly ; try something like this :

Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "trapmonkey.dtd");



--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
B

bill

Philippe Poulard said:
there is a more convenient way : JAXP provides a mean to set the public
and system id directly ; try something like this :

Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "trapmonkey.dtd");

Thanks! That last piece solved the problem (I didn't try your first
suggestion). I spent days researching this. What a relief! Thanks
again, Philippe!

Bill
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top