Java XML Node to String toString(Node node, StringBuilder sb)

Here you can find the source of toString(Node node, StringBuilder sb)

Description

to String

License

Apache License

Declaration

public static void toString(Node node, StringBuilder sb) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static String toString(Node node) {
        StringBuilder sb = new StringBuilder();
        toString(node, sb);/*from   w  ww .  ja v a  2  s . c om*/
        return sb.toString();
    }

    public static void toString(Node node, StringBuilder sb) {
        sb.append(node.getClass().getSimpleName());
        if (node instanceof Element) {

            Element el = (Element) node;
            sb.append(":");
            sb.append(el.getNodeName());
            sb.append(" ");
            attrToString(el, sb);
        }
    }

    public static void attrToString(Element el, StringBuilder sb) {
        if (el.hasAttributes() == false) {
            return;
        }
        NamedNodeMap attrs = el.getAttributes();
        for (int i = 0; i < attrs.getLength(); ++i) {
            sb.append(" ");
            Node att = attrs.item(i);
            sb.append(att);

        }
    }
}

Related

  1. toString(Node node)
  2. toString(Node node, boolean formatted)
  3. toString(Node node, int level, boolean indent)
  4. toString(Node node, Map outputProperties)
  5. toString(Node node, Map outputProperties)
  6. toString(Node xml)
  7. toString(NodeList nodes)
  8. toStringE(Node element)
  9. toText(Node node)