I'm using a home grown serializer (DOMWriter) to write my xml to a file.  I'm curious as to what other people are using.

Basically I use the following print method and helper method normalize.

 

public void print(Node node) throws DoraException

    {

 

        // is there anything to do?

        if (node == null)

        {

            return;

        }

 

        int type = node.getNodeType();

 

        switch (type)

        {

            // print document

            case Node.DOCUMENT_NODE:

                {

                    if (!canonical)

                    {

                        String  Encoding = DOMWriter.getWriterEncoding();

 

                        if (Encoding.equalsIgnoreCase("DEFAULT"))

                        {

                            Encoding = "UTF-8";

                        } else

                        if (Encoding.equalsIgnoreCase("Unicode"))

                        {

                            Encoding = "UTF-16";

                        } else

                        {

                            Encoding = MIME2Java.reverse(Encoding);

                        }

 

                        out.println("<?xml version=\"1.0\" encoding=\""

                                + Encoding + "\"?>");

                    }

                    // print(((Document)node).getDocumentElement());

 

                    NodeList children = node.getChildNodes();

 

                    for (int iChild = 0; iChild < children.getLength(); iChild++)

                    {

                        print(children.item(iChild));

                    }

                    out.flush();

                    break;

                }

 

            // print element with attributes

            case Node.ELEMENT_NODE:

                {

                    out.print('<');

                    out.print(node.getNodeName());

                    Attr attrs[] = sortAttributes(node.getAttributes());

 

                    for (int i = 0; i < attrs.length; i++)

                    {

                        Attr attr = attrs[i];

 

                        out.print(' ');

                        out.print(attr.getNodeName());

                        out.print("=\"");

                        out.print(normalize(attr.getNodeValue(), canonical));

                        out.print('"');

                    }

                    out.print('>');

                    NodeList children = node.getChildNodes();

 

                    if (children != null)

                    {

                        int len = children.getLength();

 

                        for (int i = 0; i < len; i++)

                        {

                            print(children.item(i));

                        }

                    }

                    break;

                }

 

            // handle entity reference nodes

            case Node.ENTITY_REFERENCE_NODE:

                {

                    if (canonical)

                    {

                        NodeList children = node.getChildNodes();

 

                        if (children != null)

                        {

                            int len = children.getLength();

 

                            for (int i = 0; i < len; i++)

                            {

                                print(children.item(i));

                            }

                        }

                    } else

                    {

                        out.print('&');

                        out.print(node.getNodeName());

                        out.print(';');

                    }

                    break;

                }

 

            // print cdata sections

            case Node.CDATA_SECTION_NODE:

                {

                    if (canonical)

                    {

                        out.print(normalize(node.getNodeValue(), canonical));

                    } else

                    {

                        out.print("<![CDATA[");

                        out.print(node.getNodeValue());

                        out.print("]]>");

                    }

                    break;

                }

 

            // print text

            case Node.TEXT_NODE:

                {

                    // out.print(node.getNodeValue());

                    out.print(normalize(node.getNodeValue(), canonical));

                    break;

                }

 

            // print processing instruction

            case Node.PROCESSING_INSTRUCTION_NODE:

                {

                    out.print("<?");

                    out.print(node.getNodeName());

                    String data = "">

 

                    if (data != null && data.length() > 0)

                    {

                        out.print(' ');

                        out.print(data);

                    }

                    out.print("?>");

                    break;

                }

        }

 

        if (type == Node.ELEMENT_NODE)

        {

            out.print("</");

            out.print(node.getNodeName());

            out.print('>');

        }

 

        out.flush();

 

    } // print(Node)

 

 

public static String normalize(String s, boolean canonical)

    {

        StringBuffer str = new StringBuffer();

 

        int len = (s != null) ? s.length() : 0;

 

        for (int i = 0; i < len; i++)

        {

            char ch = s.charAt(i);

 

            switch (ch)

            {

                case '<':

                    {

                        str.append("&lt;");

                        break;

                    }

 

                case '>':

                    {

                        str.append("&gt;");

                        break;

                    }

 

                case '&':

                    {

                        str.append("&amp;");

                        break;

                    }

 

                case '"':

                    {

                        str.append("&quot;");

                        break;

                    }

 

                case '\'':

                    {

                        str.append("&apos;");

                        break;

                    }

/*

                case '\r':

                case '\n':

                    {

                        if (canonical)

                        {

                            str.append("&#");

                            str.append(Integer.toString(ch));

                            str.append(';');

                            break;

                        }

                        // else, default append char

                    }

*/

                default:

                    {

                        str.append(ch);

                    }

            }

        }

 

        return(str.toString());

 

    } // normalize(String):String

This electronic transmission is strictly confidential to Smith & Nephew and intended solely for the addressee. It may contain information which is covered by legal, professional or other privilege. If you are not the intended addressee, or someone authorized by the intended addressee to receive transmissions on behalf of the addressee, you must not retain, disclose in any form, copy or take any action in reliance on this transmission. If you have received this transmission in error, please notify the sender as soon as possible and destroy this message.

Reply via email to