Java XML String Transform getHtmlAsString(Node node)

Here you can find the source of getHtmlAsString(Node node)

Description

get Html As String

License

Open Source License

Declaration

public static String getHtmlAsString(Node node) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    public static String getHtmlAsString(Node node) {
        String s = "";
        try {// w  ww .j  a  v  a  2 s. c  o m
            s = getXmlAsString(node);
        } catch (TransformerException e) {
        }
        return s.toLowerCase().replaceAll(" xmlns=\"http://www.w3.org/1999/xhtml\"", "");
    }

    /**
     * @param node
     * @return output of the node's xml string.
     * @throws TransformerException
     */
    public static String getXmlAsString(Node node) throws TransformerException {
        Transformer transformer = getTransformer();
        DOMSource source = new DOMSource(node);
        StringWriter xmlString = new StringWriter();
        StreamResult streamResult = new StreamResult(xmlString);
        transformer.transform(source, streamResult);
        return xmlString.toString();
    }

    private static Transformer getTransformer() throws TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        return transformer;
    }

    /**
     * 
     * @param node
     * @return
     * @deprecated Use {@link #getXmlAsString(Node)} instead.
     */
    public static String toString(Node node) {
        StringBuffer sb = new StringBuffer();
        if (node instanceof Text) {
            if (node.getNodeValue() != null) {
                byte[] bytes = node.getNodeValue().getBytes();
                List<Byte> newBytes = new ArrayList<Byte>();
                for (int i = 0; i < bytes.length; i++) {
                    if (bytes[i] == 63) {
                        byte[] bs = "&nbsp;".getBytes();
                        for (byte b : bs) {
                            newBytes.add(b);
                        }
                    } else {
                        newBytes.add(bytes[i]);
                    }
                }
                byte[] valueBytes = new byte[newBytes.size()];
                for (int i = 0; i < newBytes.size(); i++) {
                    valueBytes[i] = newBytes.get(i).byteValue();
                }
                sb.append(new String(valueBytes));
            }
        } else {
            sb.append("<").append(node.getNodeName());
            NamedNodeMap attrs = node.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                sb.append(" ").append(attrs.item(j).getNodeName()).append("=\"")
                        .append(attrs.item(j).getNodeValue()).append("\"");
            }
            sb.append(">");
            if (node.hasChildNodes()) {
                NodeList children = node.getChildNodes();
                for (int i = 0; i < children.getLength(); i++) {
                    String str = toString(children.item(i));
                    if (str != null)
                        sb.append(str);
                }
            } else {
                sb.append(node.getNodeValue());
            }
            sb.append("</").append(node.getNodeName()).append(">");
        }
        return sb.toString();
    }
}

Related

  1. extractText(Node n, StringBuffer buf)
  2. fileToXMLString(String filename)
  3. getAsXMLString(final Node node)
  4. getBeanDefinition(String beanId, File file)
  5. getConfigurationAsXML(Map properties)
  6. getIntegerAttribute(XMLStreamReader in, String attribute)
  7. getNode(String text)
  8. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  9. getOutputMimeType(String xslName)