Java XML Attribute Append appendAllAttributes(Node node, StringBuffer xpath)

Here you can find the source of appendAllAttributes(Node node, StringBuffer xpath)

Description

append All Attributes

License

Open Source License

Declaration

public static void appendAllAttributes(Node node, StringBuffer xpath) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static void appendAllAttributes(Node node, StringBuffer xpath) {
        NamedNodeMap attr = node.getAttributes();
        int len = attr.getLength();
        if (len > 0) {
            for (int i = 0; i < len; i++) {
                Node item = attr.item(i);
                xpath.append("[@");
                xpath.append(item.getNodeName());
                xpath.append("='");
                xpath.append(item.getNodeValue());
                xpath.append("']");
            }/*  w  w  w . j  a va 2  s  .  co m*/
        }
    }

    /**
     * Utility method to fetch the value of the element node
     *
     * @param node
     * @return
     */
    public static String getNodeValue(Node node) {
        for (Node child = node.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child.getNodeValue();
            }
        }
        return null;
    }
}

Related

  1. appendAttribute(Node node, String name, String value)
  2. appendAttributeIfNotNull(Element parentElement, String attributeName, Object attributeValue)
  3. appendAttributeNode(String namespace, Element parent, String name, String value)
  4. appendAttributes(Node node, StringBuffer sb)