DOCTYPE not created

Discussion in 'Java' started by bill turner, Oct 20, 2004.

  1. bill turner

    bill turner Guest

    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);
     
    bill turner, Oct 20, 2004
    #1
    1. Advertising

  2. bill turner

    Richard Guest

    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) ;

    =========================

    On Wed, 20 Oct 2004 17:21:17 GMT, bill turner <>
    wrote:

    >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();
    >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
     
    Richard, Oct 21, 2004
    #2
    1. Advertising

  3. bill turner

    Richard Guest

    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) ;

    =========================


    On Wed, 20 Oct 2004 17:21:17 GMT, bill turner <>
    wrote:

    >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();
    >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
     
    Richard, Oct 21, 2004
    #3
  4. bill turner wrote:
    > 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 |
    -----------------------
     
    Philippe Poulard, Oct 21, 2004
    #4
  5. Philippe Poulard wrote:
    > bill turner wrote:
    >
    >> 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
    >


    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");


    >> 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 |
    -----------------------
     
    Philippe Poulard, Oct 21, 2004
    #5
  6. bill turner

    bill Guest

    Philippe Poulard <> wrote in message news:<cl7ph8$o6l$>...
    > Philippe Poulard wrote:
    > > bill turner wrote:
    > >
    > >> 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
    > >

    >
    > 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");
    >
    >
    > >> 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

    > >
    > >
    > >


    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
     
    bill, Oct 21, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ulf Heyder
    Replies:
    0
    Views:
    591
    Ulf Heyder
    Oct 16, 2003
  2. bill turner

    DOCTYPE not created

    bill turner, Oct 20, 2004, in forum: XML
    Replies:
    3
    Views:
    451
  3. jimjim
    Replies:
    12
    Views:
    1,920
    Ron Natalie
    Jun 3, 2005
  4. tom c
    Replies:
    6
    Views:
    518
    tom c
    Sep 6, 2006
  5. Larry Lindstrom
    Replies:
    19
    Views:
    1,333
    Jonathan N. Little
    Jun 12, 2012
Loading...

Share This Page