Serializing an XML Dom

Discussion in 'XML' started by Tjerk Wolterink, Dec 1, 2005.

  1. I have created an W3C XML DOM in java,
    it uses namespaces.

    Now i want to serialize it to the outputstream:

    ===
    Document document=retrievePageDocument(page, manager.getDataStore());
    response.setContentType("text/xml");
    PrintWriter out=response.getWriter();

    DOMSource domSource=new DOMSource(document);

    StreamResult streamResult=new StreamResult(out);
    TransformerFactory tf=TransformerFactory.newInstance();
    Transformer serializer=tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    serializer.transform(domSource, streamResult);
    ===

    The serialization works, but it does not include the namespaces!!

    I also tried to use the Xerces Serialization API directly:

    ===
    // lets serialize the xml to the output
    XMLSerializer serializer=new XMLSerializer(out,
    new OutputFormat(document, "UTF-8", true)
    );
    serializer.serialize(document);
    ===

    But that also does not inlcude the namespaces??@?

    The Document object is namespace aware:

    ===
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Document doc;
    doc=factory.newDocumentBuilder().newDocument();
    Element root=doc.createElementNS(PAGE_NAMESPACE, "page");
    doc.appendChild(root);
    ===


    Pleaz help im stuck here!
     
    Tjerk Wolterink, Dec 1, 2005
    #1
    1. Advertising

  2. Tjerk Wolterink wrote:

    > I have created an W3C XML DOM in java,
    > it uses namespaces.


    > The serialization works, but it does not include the namespaces!!


    The following

    DocumentBuilderFactory documentBuilderFactory =
    DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder =
    documentBuilderFactory.newDocumentBuilder();
    Document xmlDocument =
    documentBuilder.getDOMImplementation().createDocument(
    "http://www.w3.org/1999/xhtml",
    "html",
    null
    );
    Element rootElement = xmlDocument.getDocumentElement();

    rootElement.setAttributeNS("http://www.w3.org/XML/1998/namespace",
    "xml:lang", "en");

    System.out.println("xmlDocument class: " +
    xmlDocument.getClass().getName());
    System.out.println("Serialized document:");
    System.out.println(serializeToString(xmlDocument));

    run with Java 1.5 gives

    xmlDocument class: com.sun.org.apache.xerces.internal.dom.DocumentImpl
    Serialized document:
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"></html>

    which looks fine, namespace declarations and prefixes are there.

    Run with Java 1.4 however I get:

    xmlDocument class: org.apache.crimson.tree.XmlDocument
    Serialized document:
    <?xml version="1.0" encoding="UTF-8"?>
    <html xml:lang="en"/>

    so you might have hit a bug with the DOM or Transformer implementation
    you are using respectively which is on your classpath.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 2, 2005
    #2
    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. Michael
    Replies:
    1
    Views:
    5,364
    Michael Berg
    Feb 22, 2004
  2. asd

    Serializing DOM object

    asd, Jun 8, 2005, in forum: Java
    Replies:
    3
    Views:
    439
  3. Replies:
    0
    Views:
    581
  4. Replies:
    3
    Views:
    559
    Stefan Behnel
    Aug 3, 2007
  5. Alan
    Replies:
    6
    Views:
    1,694
Loading...

Share This Page