Example usage for org.w3c.dom Attr getNodeValue

List of usage examples for org.w3c.dom Attr getNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr getNodeValue.

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  ww  w  . ja v a 2  s.  c  o m
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        String attrValue = attr.getNodeValue();
    }
}

From source file:Main.java

public static Map<String, String> getAttributes(Node n) {
    if (n == null)
        return null;
    NamedNodeMap attributes = n.getAttributes();
    if (attributes == null || attributes.getLength() <= 0)
        return null;

    Map<String, String> result = new HashMap<>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        result.put(attr.getNodeName(), attr.getNodeValue());
    }//from w w  w.j a  v a2  s  .  co  m
    return result;
}

From source file:Main.java

public static String getAttrNoEx(Node node, String name) {
    Attr attr = ((Element) node).getAttributeNode(name);
    if (attr == null)
        return null;

    return attr.getNodeValue();
}

From source file:Main.java

License:asdf

public static void dom(Element element) {
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        System.out.println(attrName);
        String attrValue = attr.getNodeValue();
        System.out.println(attrValue);
    }//  ww w  .  j  a v a 2s . co  m
}

From source file:Main.java

public static ArrayList<String> getAlternativeIDs(Node xmlSubstanceNode) {
    ArrayList<String> result = new ArrayList<String>();
    int maxID = xmlSubstanceNode.getAttributes().getLength();
    for (int i = 0; i < maxID; i++) {
        Attr oAlternative = (Attr) xmlSubstanceNode.getAttributes().getNamedItem("name" + i);
        if (oAlternative != null) {
            result.add(oAlternative.getNodeValue());
        }// w  w  w .  j a  va 2s .c om
    }
    return result;
}

From source file:Main.java

public static int getAlternativeIdCount(Node xmlSubstanceNode, boolean includeEmpty) {
    int idCnt = 0;
    int maxID = xmlSubstanceNode.getAttributes().getLength();
    for (int i = 1; i < maxID; i++) {
        Attr oAlternative = (Attr) xmlSubstanceNode.getAttributes().getNamedItem("name" + i);
        if (oAlternative != null) {
            String value = oAlternative.getNodeValue();
            if (includeEmpty || (value != null && value.length() > 0))
                idCnt++;/*from  w w  w .ja  v  a 2  s  .c  o  m*/
        }
    }
    return idCnt;
}

From source file:Main.java

/**
 *  Copy the attributes from n2 to n1./*ww w . j a  v a  2s .c o  m*/
 *
 *  @param n1 The source of the attributes.
 *  @param n2 What to copy into.
 */
public static void mergeAttributes(Element n1, Element n2) {
    if ((n1 == null) || (n2 == null)) {
        return;
    }
    NamedNodeMap nnm = n2.getAttributes();
    if (nnm == null) {
        return;
    }
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        n1.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

}

From source file:Main.java

public static String getPrefixNS(String uri, Node e) {
    while (e != null && e.getNodeType() == Node.ELEMENT_NODE) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name = a.getName();
            if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring("xmlns:".length());
            }//from  w w  w . j  a  v a2 s .co m
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object.
   @param pElement An <I>org.w3c.dom.Element</I> object to be serialized to a <I>String</I>.
   @return <I>String</I> representation of the <I>Element</I>.
 *///  w  ww  .  ja  v a2  s .  c o m
public static String convertElementToString(Element pElement) {
    // Local variables.
    int nCount = 0;

    // Start the element.
    String strName = pElement.getNodeName();
    String strReturn = "<" + strName;

    // Get the list of attributes.
    NamedNodeMap pNodeMap = pElement.getAttributes();
    nCount = pNodeMap.getLength();

    // Build the attributes.
    for (int i = 0; i < nCount; i++) {
        Attr pAttr = (Attr) pNodeMap.item(i);
        strReturn += " " + pAttr.getNodeName() + "=\"" + pAttr.getNodeValue() + "\"";
    }

    // Get the list of children.
    NodeList pChildren = pElement.getChildNodes();
    nCount = pChildren.getLength();

    // If no children exist, return a single node.
    if (0 == nCount)
        return strReturn + " />";

    // Close node.
    strReturn += ">";

    // Build out the children nodes.
    for (int i = 0; i < nCount; i++) {
        Node pChild = pChildren.item(i);

        if (pChild instanceof CharacterData)
            strReturn += ((CharacterData) pChild).getData();
        else if (pChild instanceof Element)
            strReturn += convertElementToString((Element) pChild);
    }

    return strReturn + "</" + strName + ">";
}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from   www .  ja va 2  s . c  o  m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}