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

/**
 * Find all attribute in a node with specific prefix.
 *
 * @param node         the note that contains the attributes
 * @param prefix       the prefix to search for
 * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map)
 * @return the values or a empty map/*from w ww . ja  va2  s. c o m*/
 */
public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) {
    final HashMap<String, String> result = new HashMap<>();

    final NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        final Node att = attributes.item(i);
        String name = att.getNodeName();
        if (name.startsWith(prefix)) {
            if (removePrefix) {
                name = name.substring(prefix.length());
            }
            result.put(name, att.getNodeValue());
        }
    }
    return result;
}

From source file:org.aectann.postage.TrackingStatusRefreshTask.java

public static TrackingInfo syncRequest(Context context, String tracking) throws FactoryConfigurationError {
    TrackingInfo result = null;/*from w  ww  .ja va  2  s .  com*/
    if (tracking != null && tracking.length() > 0) {
        tracking = tracking.toUpperCase();
        HttpClient client = new DefaultHttpClient();
        try {
            HttpResponse response = client.execute(new HttpGet("http://prishlo.li/" + tracking + ".xml"));
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream content = response.getEntity().getContent();
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(content);
                String weight = getFirstVaueOrNull(document, "weight");
                String from = getFirstVaueOrNull(document, "from");
                String kind = getFirstVaueOrNull(document, "kind");
                TrackingInfo old = TrackingStorageUtils.loadStoredTrackingInfo(tracking, context);
                result = new TrackingInfo(old != null ? old.getName() : null, tracking, weight, kind, from);
                NodeList checkpoints = document.getElementsByTagName("checkpoint");

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                for (int i = 0; i < checkpoints.getLength(); i++) {
                    Node current = checkpoints.item(i);
                    NamedNodeMap attributes = current.getAttributes();
                    Node state = attributes.getNamedItem("state");
                    Node attribute = attributes.getNamedItem("attribute");
                    Node date = attributes.getNamedItem("date");

                    String dateString = date.getNodeValue();
                    String attributeString = attribute.getNodeValue();
                    String stateString = state.getNodeValue();
                    String locationString = current.getFirstChild().getNodeValue();
                    result.addStatus(new TrackingStatus(stateString, dateFormat.parse(dateString),
                            attributeString, locationString));
                }
            }
        } catch (Exception e) {
            if (result == null) {
                result = new TrackingInfo(null, tracking, null, null, null);
            }
        }
    }
    return result;
}

From source file:de.betterform.xml.xforms.model.constraints.RelevanceSelector.java

private static void addAttributes(Element relevantElement, Node instanceNode) {
    NamedNodeMap instanceAttributes = instanceNode.getAttributes();

    for (int index = 0; index < instanceAttributes.getLength(); index++) {
        Node instanceAttr = (Node) instanceAttributes.item(index);

        if (isEnabled(instanceAttr)) {
            if (instanceAttr.getNamespaceURI() == null) {
                relevantElement.setAttribute(instanceAttr.getNodeName(), instanceAttr.getNodeValue());
            } else {
                relevantElement.setAttributeNS(instanceAttr.getNamespaceURI(), instanceAttr.getNodeName(),
                        instanceAttr.getNodeValue());
            }//ww w .j  a  va  2  s .  co m
        }
    }
}

From source file:Main.java

/**
 * Searches for a particular attribute attached to a node. Returns null
 * if not found//w  w  w  .ja v  a  2s .c om
 */
public static String searchForAttribute(Node content, String attributeName) {
    if (content == null) {
        return null;
    } else if (attributeName == null) {
        return null;
    }

    Node node = content.getAttributes().getNamedItem(attributeName);

    if (node == null) {
        return null;
    } else {
        return node.getNodeValue();
    }
}

From source file:Main.java

/**
 * Retrieves the desired attribute from the given Node
 * /*from ww w  .  j  a v  a 2s .  c o  m*/
 * @param xml the Node
 * @param attr the attribute name String
 * @return the attribute value String
 **/
public final static String getAttribute(final Node xml, final String attr) {
    if (xml == null) {
        return null;
    }
    final NamedNodeMap attrs = xml.getAttributes();
    return attrs == null ? null : xmlFindTextNode(attrs.getNamedItem(attr));
}

From source file:Main.java

/**
 * Sarches for ressources that have to be downloaded and creates a list with all download links.
 *
 * @param node to check for download links
 * @return list with all found download links
 *///  w  ww.  j a  v a 2s  .c  o  m
private static ArrayList<String> findUrls(Node node) {

    int type = node.getNodeType();
    ArrayList<String> result = new ArrayList<>();
    String temp = null;
    NamedNodeMap atts = node.getAttributes();
    Log.i(TAG, "parsing for ressources.  node: " + node.getNodeName() + " value: " + node.getNodeValue()
            + " atts length: " + atts.getLength() + "type: " + type);
    switch (type) {
    //Element
    case Node.ELEMENT_NODE:
        //TODO: This method is stupid. It just looks for
        // attributes named ressourcepath. What if we have to download several ressources?

        for (int j = 0; j < atts.getLength(); j++) {
            Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue());
            temp = atts.item(j).getNodeValue();

            if (temp != null) {
                result.add(temp);
                Log.i(TAG, "added path: " + temp);
            }

            Log.i(TAG, "parent node:" + node.getParentNode().getNodeName());
            Element parent = (Element) node.getParentNode();
            parent.setAttribute("TESTITEST", "dllink");

        }
        // get the pages, means the children
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            ArrayList<String> childres = findUrls(child);
            if (childres.size() > 0) {
                result.addAll(childres);
            }
        }
        break;
    }
    return result;
}

From source file:Main.java

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

    NamedNodeMap l_nnm = null;/* w w  w  .  j  a  va  2  s  . co m*/
    Node l_n = null;
    String l_an = null;
    String l_av = null;

    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();
            p_map.put(l_an, l_av);
        }
    }

}

From source file:Main.java

private static void print(Node e, String tab) {

    if (e.getNodeType() == Node.TEXT_NODE) {
        System.out.println(tab + e.getNodeValue());
        return;//from  w  ww  .j a v  a2s  .  c o m
    }

    System.out.print(tab + e.getNodeName());

    NamedNodeMap as = e.getAttributes();
    if (as != null && as.getLength() > 0) {
        System.out.print(" attributes=[");
        for (int i = 0; i < as.getLength(); i++)
            System.out.print((i == 0 ? "" : ", ") + as.item(i));
        System.out.print("]");
    }
    System.out.println();

    if (e.getNodeValue() != null)
        System.out.println(tab + " " + e.getNodeValue());

    NodeList childs = e.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++)
        print(childs.item(i), tab + " ");
}

From source file:Main.java

/**
 * Puts the node attributes (if it has any) into HashMap<String,String>
 * /* w  ww .  j a  va2  s .  com*/
 * @param node : XML node to look for its attributes
 * @return Hashmap<String,String> of the node attributes
 */
public static HashMap<String, String> getAttributes(Node node) {
    String attrName = null;
    String attrValue = null;
    HashMap<String, String> attributesMap = new HashMap<String, String>();
    NamedNodeMap attributes = node.getAttributes();
    for (int attrIndex = 0; attributes != null && attrIndex < attributes.getLength(); attrIndex++) {
        attrName = attributes.item(attrIndex).getNodeName();
        attrValue = attributes.item(attrIndex).getNodeValue();
        attributesMap.put(attrName, attrValue);
    }
    return attributesMap.size() == 0 ? null : attributesMap;
}

From source file:Main.java

/**
 * Constructs a XPath query to the supplied node.
 * /*from  w w  w . j a  v  a  2  s. c o m*/
 * @param n
 * @return
 */
public static String getXPath(Node n) {
    if (null == n) {
        throw new IllegalArgumentException("Invalid node");
    }

    ArrayList<Node> hierarchy = new ArrayList<Node>();
    StringBuffer buffer = new StringBuffer();
    Node parent = null;

    // Push parent element's on stack
    hierarchy.add(n);
    parent = n.getParentNode();
    while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) {
        hierarchy.add(0, parent);
        parent = parent.getParentNode();
    }

    Iterator<Node> i = hierarchy.iterator();
    while (i.hasNext()) {
        Node node = i.next();
        buffer.append("/");
        buffer.append(node.getNodeName());
        if (node.hasAttributes()) {
            Node uuid = node.getAttributes().getNamedItem("uuid");
            if (uuid != null) {
                buffer.append("[@uuid='");
                buffer.append(uuid.getNodeValue());
                buffer.append("']");
            }
        }
    }

    // return buffer
    return buffer.toString();
}