Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    DocumentBuilderFactory and TransformerFactory

    I saw some coding like this:

    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();

    Document doc = builder.newDocument();


    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer t = tf.newTransformer();
    t.setOutputProperty("encoding","UTF-8");

    t.transform(new DOMSource(doc),new StreamResult(baos)); String xmlString = new String(baos.toByteArray(), "utf8");

    My questions are:

    1) Does the doc (Document)
    contains the such header as:

    <?xml version="1.0" ?>

    2) Must the doc object host an XML string, or it can contain any type string(i.e., not necessarily an XML string)?

    3) What is the purpose of using the transformer to transform the doc as:

    t.transform(new DOMSource(*doc*),new StreamResult(*baos*));

    4) Does the Transformer add the header <?xml version="1.0" ?> to the baos object?


    Thanks


    Scott

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by otnj2ee View Post
    1) Does the doc (Document)
    contains the such header as:

    <?xml version="1.0" ?>
    thats the processing instruction that identifies that the content following is xml

    Quote Originally Posted by otnj2ee View Post
    2) Must the doc object host an XML string, or it can contain any type string(i.e., not necessarily an XML string)?
    doc can hold any SGML content which means xml or html in the form of elements, attributes and text-nodes

    Quote Originally Posted by otnj2ee View Post
    3) What is the purpose of using the transformer to transform the doc as:

    t.transform(new DOMSource(*doc*),new StreamResult(*baos*));
    to print out the content of the xml document in doc
    Quote Originally Posted by otnj2ee View Post
    4) Does the Transformer add the header <?xml version="1.0" ?> to the baos object?
    yes
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks for the response.

    A further question (ajax RELATED):

    I also saw some codes like this:

    (I)

    public void test(...){
    ...
    t.transform(new DOMSource(doc),new StreamResult(baos));
    //The transform(...) is the last line of the function
    }


    (II)

    public void test(...){
    ...
    t.transform(new DOMSource(doc),new StreamResult(baos));
    String xmlString = new String(baos.toByteArray(), "utf8");

    response.getOutputStream().println(xmlString);[/B]

    //The .println(...) is the last line of the function
    }


    So, what is the difference between (I) and (II)? I mean in (I) the document is returned as a String via t.transform(new DOMSource(doc),new StreamResult(baos)); While in (II) the String is returned via response.getOutputStream().println(xmlString). Why so?
    Last edited by otnj2ee; 01-14-2008 at 09:02 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •