Java XML Node to String getDescriptionOfNode(Node node)

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

Description

get Description Of Node

License

Open Source License

Declaration

public static String getDescriptionOfNode(Node node) 

Method Source Code


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

import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

public class Main {
    public static String getDescriptionOfNode(Node node) {
        if (!(node instanceof Element)) {
            return domToString(node);
        }//from  w  ww.ja  v a2s  .co m

        final String id = ((Element) node).getAttribute("id");
        if ((id == null) || (id.length() == 0)) {
            return domToString(node);
        }
        return "Node: id=" + id;

    }

    static String domToString(Node node) {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(node), new StreamResult(writer));
            return writer.toString();
        } catch (TransformerException te) {
            return "(TransformerException)";
        }
    }
}

Related

  1. createXmlString(Node node)
  2. dump(Node node)
  3. dumpNode(Node node)
  4. elementToString(Node element)
  5. getAttributeValue(Node node, String attrName)
  6. getNodeAsInputStream(Node node)
  7. getNodeAsStream(Node node)
  8. getNodeSubtreeXMLString(final Node node)
  9. getNodeXml(Node node)