把XML文档放入StringBuffer之中

public static void writeXMLFormatString(StringBuffer fileBuffer, Node node,
            int depth) {
        depth++;
        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE:
            fileBuffer.append("<?xml version=\"1.0\" encoding='UTF-8'?>");
            writeXMLFormatString(fileBuffer, ((Document) node)
                    .getDocumentElement(), depth);
            break;
        case Node.ELEMENT_NODE:
            fileBuffer.append("\r\n");
            fileBuffer.append(getSpace(depth) + "<");
            fileBuffer.append(node.getNodeName());
            org.w3c.dom.NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                fileBuffer.append(" " + attr.getNodeName() + "=\""
                        + attr.getNodeValue() + "\"");
                // if (attr.getNodeValue().equals("null"))
                // m_lastIsAString = true;
            }
            org.w3c.dom.NodeList children = node.getChildNodes();
            // fileBuffer.append(">");
            if (children != null) {
                int len = children.getLength();
                if (len > 0)
                    fileBuffer.append(">");
                else {
                    fileBuffer.append(" />");
                    m_hasOnlyAtrr = true;
                }
                for (int i = 0; i < len; i++)
                    writeXMLFormatString(fileBuffer, children.item(i), depth);
            }

            break;
        case Node.ENTITY_REFERENCE_NODE:
            fileBuffer.append("&");
            fileBuffer.append(node.getNodeName());
            fileBuffer.append(";");
            break;
        case Node.CDATA_SECTION_NODE:
            fileBuffer.append(getSpace(depth) + "<![CDATA[");
            fileBuffer.append(node.getNodeValue());
            fileBuffer.append("]]>");
            break;
        case Node.TEXT_NODE:
            fileBuffer.append(node.getNodeValue());
            m_lastIsAString = true;
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            fileBuffer.append(getSpace(depth) + "<?");
            fileBuffer.append(node.getNodeName());
            String data = node.getNodeValue();
            {
                fileBuffer.append("");
                fileBuffer.append(data);
            }
            fileBuffer.append("?>");
            break;
        }
        if (type == Node.ELEMENT_NODE) {
            if (!m_hasOnlyAtrr) {
                if (!m_lastIsAString) {
                    fileBuffer.append("\r\n");
                    fileBuffer.append(getSpace(depth) + "</");
                } else
                    fileBuffer.append("</");
                fileBuffer.append(node.getNodeName());
                fileBuffer.append('>');
                m_lastIsAString = false;
            } else {
                m_hasOnlyAtrr = false;
            }
        }
    }

    public static void writeXMLFormatString(StringBuffer fileBuffer, Node node,
            int depth, String encode) {
        depth++;
        int type = node.getNodeType();
        switch (type) {
        case Node.DOCUMENT_NODE:
            fileBuffer.append("<?xml version=\"1.0\" encoding='" + encode
                    + "'?>");
            writeXMLFormatString(fileBuffer, ((Document) node)
                    .getDocumentElement(), depth);
            break;
        case Node.ELEMENT_NODE:
            fileBuffer.append("\r\n");
            fileBuffer.append(getSpace(depth) + "<");
            fileBuffer.append(node.getNodeName());
            org.w3c.dom.NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                fileBuffer.append(" " + attr.getNodeName() + "=\""
                        + attr.getNodeValue() + "\"");
                // if (attr.getNodeValue().equals("null"))
                // m_lastIsAString = true;
            }
            org.w3c.dom.NodeList children = node.getChildNodes();
            // fileBuffer.append(">");
            if (children != null) {
                int len = children.getLength();
                if (len > 0)
                    fileBuffer.append(">");
                else {
                    fileBuffer.append(" />");
                    m_hasOnlyAtrr = true;
                }
                for (int i = 0; i < len; i++)
                    writeXMLFormatString(fileBuffer, children.item(i), depth);
            }

            break;
        case Node.ENTITY_REFERENCE_NODE:
            fileBuffer.append("&");
            fileBuffer.append(node.getNodeName());
            fileBuffer.append(";");
            break;
        case Node.CDATA_SECTION_NODE:
            fileBuffer.append(getSpace(depth) + "<![CDATA[");
            fileBuffer.append(node.getNodeValue());
            fileBuffer.append("]]>");
            break;
        case Node.TEXT_NODE:
            fileBuffer.append(node.getNodeValue());
            m_lastIsAString = true;
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            fileBuffer.append(getSpace(depth) + "<?");
            fileBuffer.append(node.getNodeName());
            String data = node.getNodeValue();
            {
                fileBuffer.append("");
                fileBuffer.append(data);
            }
            fileBuffer.append("?>");
            break;
        }
        if (type == Node.ELEMENT_NODE) {
            if (!m_hasOnlyAtrr) {
                if (!m_lastIsAString) {
                    fileBuffer.append("\r\n");
                    fileBuffer.append(getSpace(depth) + "</");
                } else
                    fileBuffer.append("</");
                fileBuffer.append(node.getNodeName());
                fileBuffer.append('>');
                m_lastIsAString = false;
            } else {
                m_hasOnlyAtrr = false;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_35427589/article/details/53501430