Java Utililty Methods XML Node Serialize

List of utility methods to do XML Node Serialize

Description

The list of methods to do XML Node Serialize are organized into topic(s).

Method

StringserializeDom(Node domElement)
serialize Dom
Document document = domElement.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", false);
return serializer.writeToString(domElement);
StringBufferserializeDom(Node node, StringBuffer writeString)
Serializes the DOM-tree to a stringBuffer.
int type = node.getNodeType();
try {
    switch (type) {
    case Node.DOCUMENT_NODE: {
        writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        writeString = serializeDom(((Document) node).getDocumentElement(), writeString);
        break;
    case Node.ELEMENT_NODE: {
        writeString.append("<");
        writeString.append(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            String outString = " " + attr.getNodeName() + "=\""
                    + replaceSpecialCharacters(attr.getNodeValue()) + "\"";
            writeString.append(outString);
        writeString.append(">");
        NodeList children = node.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                writeString = serializeDom(children.item(i), writeString);
        break;
    case Node.ENTITY_REFERENCE_NODE: {
        String outString = "&" + node.getNodeName() + ";";
        writeString.append(outString);
        break;
    case Node.CDATA_SECTION_NODE: {
        String outString = "<![CDATA[" + node.getNodeValue() + "]]>";
        writeString.append(outString);
        break;
    case Node.TEXT_NODE: {
        writeString.append(replaceSpecialCharacters(node.getNodeValue()));
        break;
    case Node.PROCESSING_INSTRUCTION_NODE: {
        String data = node.getNodeValue();
        String outString = "<?" + node.getNodeName() + " " + data + "?>";
        writeString.append(outString);
        break;
    if (type == Node.ELEMENT_NODE) {
        String outString = "</" + node.getNodeName() + ">";
        writeString.append(outString);
} catch (Exception e) {
return writeString;
StringserializeElement(Element element)
serialize Element
String serializedElement = null;
if (element != null) {
    StringWriter output = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(element), new StreamResult(output));
    serializedElement = output.toString();
return serializedElement;
...
StringserializeElement(Element element, boolean omitXmlDeclaration, boolean indent, String method)
serialize Element
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
transformer.setOutputProperty(OutputKeys.METHOD, method);
transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(element), new StreamResult(writer));
...
voidserializeNode(Node node)
serialize Node
if (writer == null)
    writer = new BufferedWriter(new OutputStreamWriter(System.out));
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
    Document doc = (Document) node;
    writer.write("<?xml version=\"");
    writer.write(doc.getXmlVersion());
    writer.write("\" encoding=\"UTF-8\" standalone=\"");
...
StringserializeNode(Node node)
serialize Node
String serializedElement = null;
if (node != null) {
    DOMImplementationLS impl = getDOMImplementationLS(node);
    LSSerializer writer = impl.createLSSerializer();
    serializedElement = writer.writeToString(node);
return serializedElement;
StringserializeNode(Node node)
serialize Node
StringBuffer buffer = new StringBuffer();
String nodeType = null;
Short nodeTypeNum = node.getNodeType();
switch (nodeTypeNum) {
case Node.ATTRIBUTE_NODE:
    nodeType = "ATTRIBUTE";
    break;
case Node.CDATA_SECTION_NODE:
...
StringserializeNode(Node node)
Serialize the given DOM node.
return new String(serializeNode(node, "UTF-8", false), "UTF-8");
StringserializeNode(Node node)
serialize Node
String serializedElement = null;
if (node != null) {
    System.setProperty(DOMImplementationRegistry.PROPERTY,
            "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    serializedElement = writer.writeToString(node);
...
voidserializeNode(Node node, String filename)
serialize Node
try {
    Transformer xform = TransformerFactory.newInstance().newTransformer();
    DOMSource domSrc = new DOMSource(node);
    FileOutputStream fos = new FileOutputStream(filename);
    StreamResult streamResult = new StreamResult(fos);
    xform.transform(domSrc, streamResult);
    fos.close();
} catch (Exception e) {
...