logo       

dom serializer--what to use: msg#00024

Subject: dom serializer--what to use

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.

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
linux.arklinux....    user-groups.lin...    kde.usability/2...    ietf.ipp/2002-0...    mail.spam.spamc...    os.netbsd.devel...    audio.cd-record...    text.unicode.de...    php.documentati...    games.fps.halfl...    window-managers...    suse.oracle.gen...    bug-tracking.gn...    video.dvdrip.us...    xfree86.cvs/200...    java.netbeans.m...    network.argus/2...    culture.sf.kill...    debian.ports.al...    freebsd.questio...    qplus.devel/200...    handhelds.palm....   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe