Java XML Node to String nodeToString(Node node)

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

Description

node To String

License

Apache License

Declaration

public static String nodeToString(Node node) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

public class Main {
    public static String nodeToString(Node node) {
        String strNode = "";
        Node nodeIt = node;/* ww  w  .j a  v a  2s.  c om*/

        if (nodeIt instanceof Element) {
            Element elem = (Element) nodeIt;
            String strElem = "";
            strElem += "<" + elem.getTagName();
            NamedNodeMap attribs = elem.getAttributes();
            int len = attribs.getLength();
            for (int i = 0; i < len; i++) {
                Attr attr = (Attr) attribs.item(i);
                strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\"";
            }
            strElem += ">";
            strNode = strElem + strNode;
        } else if (nodeIt instanceof CharacterData) {
            CharacterData charNode = (CharacterData) nodeIt;
            strNode = charNode.getData() + strNode;
        } else if (nodeIt instanceof Document)
            strNode = strNode; // no hacemos nada
        else
            strNode = nodeIt.getNodeValue() + strNode;

        return strNode;
    }
}

Related

  1. nodeToString(Node n)
  2. nodeToString(Node n)
  3. nodeToString(Node n)
  4. nodeToString(Node node)
  5. nodeToString(Node node)
  6. nodeToString(Node node)
  7. nodeToString(Node node)
  8. nodeToString(Node node)
  9. nodeToString(Node node)