Example usage for org.w3c.dom Node getAttributes

List of usage examples for org.w3c.dom Node getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Node getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

public static ArrayList<HashMap<String, String>> getWhoList(Node currentNode, String tagName) {
    String result = "";
    ArrayList<HashMap<String, String>> whoArrayList = new ArrayList<HashMap<String, String>>();
    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);
        if (childNode.getNodeName().equals(tagName)) {
            HashMap<String, String> whoMap = new HashMap<String, String>();
            NamedNodeMap attrNodeMap = childNode.getAttributes();
            Node activity = attrNodeMap.getNamedItem("activity");
            Node email = attrNodeMap.getNamedItem("email");
            Node name = attrNodeMap.getNamedItem("name");
            if (activity != null) {
                whoMap.put("activity", activity.getNodeValue());
            }/*from   ww w. jav a2  s.c  om*/
            if (email != null) {
                whoMap.put("email", email.getNodeValue());
            }
            if (name != null) {
                whoMap.put("name", name.getNodeValue());
            }
            whoArrayList.add(whoMap);
        }
    }
    return whoArrayList;
}

From source file:Main.java

public static Node addAttribute(Node pNode, String attrName, String attrValue) {
    Node attributeNode = null;//from   w ww. j ava  2  s.co m
    try {
        Attr _attr = pNode.getOwnerDocument().createAttribute(attrName);
        _attr.setNodeValue(attrValue);

        attributeNode = pNode.getAttributes().setNamedItem(_attr);

    } catch (Exception e) {
        attributeNode = null;
    }

    return attributeNode;
}

From source file:Main.java

public static String[] getAllTokens(Document doc, boolean hasCorpusElement) {
    ArrayList<String> toReturnAL = new ArrayList<String>();

    if (hasCorpusElement) {

        NodeList corpusDocs = doc.getChildNodes().item(0).getChildNodes();

        for (int d = 0; d < corpusDocs.getLength(); d++) {
            if (!corpusDocs.item(d).getNodeName().equals("doc"))
                continue;

            //System.out.println(doc.getChildNodes().getLength());

            NodeList sentences = corpusDocs.item(d).getChildNodes();

            for (int i = 0; i < sentences.getLength(); i++) {
                if (!sentences.item(i).getNodeName().equals("s"))
                    continue;
                NodeList tokens = sentences.item(i).getChildNodes();
                for (int j = 0; j < tokens.getLength(); j++) {
                    Node tokenNode = tokens.item(j);
                    if (tokenNode.getNodeName().equals("toponym")) {
                        toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue());
                    } else if (tokenNode.getNodeName().equals("w")) {
                        toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue());
                    }/*from ww w  . jav  a2 s.c  o  m*/

                }
            }
        }
    } else {
        NodeList sentences = doc.getChildNodes().item(1).getChildNodes();

        for (int i = 0; i < sentences.getLength(); i++) {
            if (!sentences.item(i).getNodeName().equals("s"))
                continue;
            NodeList tokens = sentences.item(i).getChildNodes();
            for (int j = 0; j < tokens.getLength(); j++) {
                Node tokenNode = tokens.item(j);
                if (tokenNode.getNodeName().equals("toponym")) {
                    toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue());
                } else if (tokenNode.getNodeName().equals("w")) {
                    toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue());
                }

            }
        }
    }

    return toReturnAL.toArray(new String[0]);
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(Node node) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    NodeList subNodes = node.getChildNodes();

    NamedNodeMap nodeAttrs = node.getAttributes();
    for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) {
        Node attrNode = nodeAttrs.item(nodeAttrIdx);
        // nodeMap.put("@"+attrNode.getNodeName(),
        // attrNode.getTextContent());
        nodeMap.put("@" + attrNode.getNodeName(), attrNode.getNodeValue());
    }/*from ww  w .  ja v a 2  s. c om*/

    if (nodeAttrs.getLength() == 0)
        if (subNodes.getLength() == 0)
            return "";
        else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE)
            // return subNodes.item(0).getTextContent();
            return subNodes.item(0).getNodeValue();

    for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) {
        Node subNode = subNodes.item(subNodeIdx);

        if (subNode.getNodeType() == Node.TEXT_NODE) {
            // nodeMap.put(subNode.getNodeName(), subNode.getTextContent());
            nodeMap.put(subNode.getNodeName(), subNode.getNodeValue());
        } else {
            if (nodeMap.containsKey(subNode.getNodeName())) {
                Object subObject = nodeMap.get(subNode.getNodeName());
                if (subObject instanceof List<?>) {
                    ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode));
                } else {
                    List<Object> subObjectList = new ArrayList<Object>();
                    subObjectList.add(subObject);
                    subObjectList.add(transformXmlNodesIntoMap(subNode));
                    nodeMap.put(subNode.getNodeName(), subObjectList);
                }
            } else {
                nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode));
            }
        }

    }
    return nodeMap;
}

From source file:Main.java

/**
 * @param vertexStyleNode/*  ww w  .  j av  a 2s.  c  o m*/
 * @return the WKN
 */
public static String toWellKnowName(Node vertexStyleNode) {
    if (vertexStyleNode == null) {
        return "";
    }

    String value = "square";
    try {

        NamedNodeMap atts = vertexStyleNode.getAttributes();
        String nodeVal = atts.getNamedItem("class").getNodeValue();

        if (nodeVal.indexOf("Square") > -1) {
            // already there
        } else if (nodeVal.indexOf("Circle") > -1) {
            value = "circle";
        } else if (nodeVal.indexOf("Cross") > -1) {
            value = "cross";
        } else if (nodeVal.indexOf("Star") > -1) {
            value = "star";
        } else if (nodeVal.indexOf("Triangle") > -1) {
            value = "triangle";
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:XmlUtil.java

/**
 * Returns a map of all node's attributes. All non-attribute nodes are ignored.
 *///from  w w  w  .j a  v  a  2s .c  o m
public static Map<String, String> getAllAttributes(Node node) {
    HashMap<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        attrs.put(attribute.getNodeName(), attribute.getNodeValue());
    }
    return attrs;
}

From source file:Main.java

/**
 * @param n1 first Node to test/*from  w  ww  .  ja va2s.  c om*/
 * @param n2 second Node to test
 * @return true if a deep compare show the same children and attributes in the same order
 */
public static boolean equals(Node n1, Node n2) {
    // compare type
    if (!n1.getNodeName().equals(n2.getNodeName()))
        return false;
    // compare attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if (nnm1.getLength() != nnm2.getLength())
        return false;
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node attr1 = nnm1.item(i);
        if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName())))
            return false;
    }
    // compare children
    Node c1 = n1.getFirstChild();
    Node c2 = n2.getFirstChild();
    for (;;) {
        while ((c1 != null) && c1.getNodeName().startsWith("#"))
            c1 = c1.getNextSibling();
        while ((c2 != null) && c2.getNodeName().startsWith("#"))
            c2 = c2.getNextSibling();
        if ((c1 == null) && (c2 == null))
            break;
        if ((c1 == null) || (c2 == null))
            return false;
        if (!equals(c1, c2))
            return false;
        c1 = c1.getNextSibling();
        c2 = c2.getNextSibling();
    }
    return true;
}

From source file:Main.java

public static Node duplicate(Node node, Document ownerDocument) {
    if (node instanceof Text)
        return ownerDocument.createTextNode(node.getNodeValue());
    Node newNode = ownerDocument.createElement(node.getNodeName());
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Node attrib = attribs.item(i);
        addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
    }/* w w w . j a v a  2  s  .  co  m*/
    for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
        Node newN = duplicate(n, ownerDocument);
        newNode.appendChild(newN);
    }
    return newNode;
}

From source file:org.wise.vle.domain.webservice.crater.CRaterHttpClient.java

/**
 * Gets and Returns the Concepts from the CRater response XML string,
 * or "" if it does not exist.//  www .ja v a  2 s  .com
 * @param cRaterResponseXML response XML from the CRater. Looks like this:
 * <crater-results>
 *   <tracking id="1013701"/>
 *   <client id="WISETEST"/>
 *   <items>
 *     <item id="Photo_Sun">
 *     <responses>
 *       <response id="testID" score="4" concepts="1,2,3,4,5"/>
 *     </responses>
 *   </item>
 * </items>
 * 
 * @return String concepts returned from the CRater. In the case above, this method will return "1,2,3,4,5".
 */
public static String getConcepts(String cRaterResponseXML) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(cRaterResponseXML.getBytes()));
        NodeList responseList = doc.getElementsByTagName("response");
        Node response = responseList.item(0);
        if (response.getAttributes().getNamedItem("concepts") != null) {
            return response.getAttributes().getNamedItem("concepts").getNodeValue();
        } else {
            return "";
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Puts the node attributes (if it has any) into HashMap<String,String> Retrieves only those
 * attributes that are in attr ArrayList Ignores other attributes values that are in node
 * /*from   w w w .  j  a v  a 2s .co  m*/
 * @param node : XML node to look for its attributes
 * @param attrNames : attributes to fetch from node
 * @return Hashmap<String,String> of the node attributes
 */
public static HashMap<String, String> getAttributesByName(Node node, ArrayList<String> attrNames) {
    String attrName = null;
    String attrValue = null;
    HashMap<String, String> attributesMap = new HashMap<String, String>();
    NamedNodeMap attributes = node.getAttributes();
    for (int attrIndex = 0; attrIndex < attributes.getLength(); attrIndex++) {
        attrName = attributes.item(attrIndex).getNodeName();
        attrValue = attributes.item(attrIndex).getNodeValue();
        if (attrNames.contains(attrName)) {
            attributesMap.put(attrName, attrValue);
        }
    }
    return attributesMap.size() == 0 ? null : attributesMap;
}