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

private static String getNodeAttribute(Element element, String nodeWeiZhi, String attributeName) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getAttributes().getNamedItem(attributeName).getNodeValue().trim();
            }//from w  w w.  ja va 2 s.c o  m
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeAttribute((Element) node, nodeWeiZhiTemp, attributeName);
            }
        }
    }
    return result;
}

From source file:Main.java

public static HashMap<String, String> getAttributes(Node p_node) throws Exception {

    NamedNodeMap l_nnm = null;/*from  w  w  w.  j a v a  2s.com*/
    Node l_n = null;
    String l_an = null;
    String l_av = null;
    HashMap<String, String> l_a = new HashMap<String, String>();

    l_nnm = p_node.getAttributes();
    if (l_nnm != null) {
        for (int l_i = 0; l_i < l_nnm.getLength(); l_i++) {
            l_n = l_nnm.item(l_i);
            l_an = l_n.getNodeName();
            l_av = l_n.getNodeValue();
            l_a.put(l_an, l_av);
        }
    }

    return l_a;
}

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

/**
 * Gets and Returns the Score from the CRater response XML string,
 * or -1 if it does not exist.//from   w ww  .  j ava2  s  .  co m
 * @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 integer score returned from the CRater. In the case above, this method will return 4.
 */
public static int getScore(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);
        String score = response.getAttributes().getNamedItem("score").getNodeValue();
        return Integer.valueOf(score);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

public static boolean attributesExist(Node node, Map<String, String> attributesMap) {
    boolean exists = true;
    Map<String, String> nodeAttributes = new HashMap<String, String>();
    for (int i = 0; i < node.getAttributes().getLength(); i++) {
        nodeAttributes.put(node.getAttributes().item(i).getNodeName(),
                node.getAttributes().item(i).getNodeValue());
    }/*from w  w w.  j a  va2 s  .  c om*/

    if (attributesMap == null) {
        return exists;
    }

    for (String attr : attributesMap.keySet()) {
        if (nodeAttributes.get(attr) == null) {
            exists = false;
            break;
        }
        if (!nodeAttributes.get(attr).equals(attributesMap.get(attr))) {
            exists = false;
            break;
        }
    }

    return exists;
}

From source file:Main.java

/**
 *  Returns first node at the bottom of path from node.
 * If element begins with '@', indicates an attribute, eg "@id"
 * The '#text' element indicates that the node has a single text child.
 * @param node    Node to apply path to/*from   w w  w.j  a  v  a  2 s .co m*/
 * @param path    Path to apply
 * @return        Node at bottom of path, or null
 */
static public Node extractNode(Node node, String path) {
    if (node == null)
        return null;
    NodeList list = node.getChildNodes();
    if (path.equals("#text"))
        return node.getFirstChild();
    else if (path.charAt(0) == '@')
        return node.getAttributes().getNamedItem(path.substring(1));
    else
        for (int j = 0; j < list.getLength(); j++)
            if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                return list.item(j);

    return null;
}

From source file:DomUtil.java

public static String getAttribute(Node element, String attName) {
    NamedNodeMap attrs = element.getAttributes();
    if (attrs == null)
        return null;
    Node attN = attrs.getNamedItem(attName);
    if (attN == null)
        return null;
    return attN.getNodeValue();
}

From source file:Main.java

public static final Node getSubNodeById(Node n, String id) {
    int i;//from  w w w .  j av a  2 s. c o  m
    NodeList children;
    Node childnode;

    if (n == null) {
        return null;
    }

    children = n.getChildNodes();

    for (i = 0; i < children.getLength(); i++) {
        childnode = children.item(i);
        NamedNodeMap nnm = childnode.getAttributes();
        if (nnm != null) {
            Node attr = nnm.getNamedItem("id");
            if (attr != null && id.equals(attr.getNodeValue())) {
                return childnode;
            }
        }

        childnode = getSubNodeById(childnode, id);
        if (childnode != null) {
            return childnode;
        }
    }
    return null;
}

From source file:Main.java

public static String getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//w ww . j a  v a2  s .c o m

        DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = domBuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(nodeName)) {
                        returnVal = node.getAttributes().getNamedItem(attributeName).getNodeValue();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnVal;
}

From source file:Main.java

public static List<Node> getChildren(Node node) {
    ArrayList<Node> children = new ArrayList<Node>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        children.add(nodeList.item(i));/*from  w ww  .j a  v a 2 s . c o m*/
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            children.add(attributes.item(i));
        }
    }
    return children;
}

From source file:Main.java

/**
 *  Removes a named attribute of a Node<p>
 *  @param node The node to search//from  www . j a  va 2s . c o  m
 *  @param attr The name of the attribute to remove
 */
public synchronized static void removeAttribute(Node node, String attr) {
    if (node == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (attr == null)
        throw new IllegalArgumentException("Node attribute argument cannot be null");

    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        Node an = map.getNamedItem(attr);
        if (an != null)
            map.removeNamedItem(attr);
    }
}