Java XML Attribute from Node parseAttributes(Node n)

Here you can find the source of parseAttributes(Node n)

Description

parse Attributes

License

Apache License

Declaration

public static Properties parseAttributes(Node n) 

Method Source Code


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

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import java.util.Properties;

public class Main {
    public static Properties parseAttributes(Node n) {
        return parseAttributes(n, null);
    }//  www  .  j av  a  2  s .c  o  m

    public static Properties parseAttributes(Node n, Properties variables) {
        Properties attributes = new Properties();
        NamedNodeMap attributeNodes = n.getAttributes();
        for (int i = 0; i < attributeNodes.getLength(); i++) {
            Node attribute = attributeNodes.item(i);
            String value = parsePropertyTokens(attribute.getNodeValue(), variables);
            attributes.put(attribute.getNodeName(), value);
        }
        return attributes;
    }

    public static String parsePropertyTokens(String string, Properties variables) {
        final String OPEN = "${";
        final String CLOSE = "}";

        String newString = string;
        if (newString != null && variables != null) {
            int start = newString.indexOf(OPEN);
            int end = newString.indexOf(CLOSE);

            while (start > -1 && end > start) {
                String prepend = newString.substring(0, start);
                String append = newString.substring(end + CLOSE.length());
                String propName = newString.substring(start + OPEN.length(), end);
                String propValue = variables.getProperty(propName);
                if (propValue == null) {
                    newString = prepend + propName + append;
                } else {
                    newString = prepend + propValue + append;
                }
                start = newString.indexOf(OPEN);
                end = newString.indexOf(CLOSE);
            }
        }
        return newString;
    }
}

Related

  1. getXmlNodeAttributeValue(Node NN, String AttrName)
  2. getYesNoAttrVal(final NamedNodeMap nnm, final String name)
  3. hasAttribByName(Element node, String name)
  4. parseAttribute(Node lnNode, String attributeName)
  5. parseAttribute(NodeList abtList)
  6. parseAttributes(Node node)
  7. parseAttributesTag(Node n)
  8. xmlGetAttribute(Node node, String attrname)
  9. xmlGetAttribute2(Node node, String attrname)