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 void main(String argv[]) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml"));
    NodeList nodeNodeList = document.getElementsByTagName("node");
    for (int i = 0; i < nodeNodeList.getLength(); i++) {
        Node nNode = nodeNodeList.item(i);
        System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
        System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());
    }/*from  ww w . j  a v  a  2 s.c  om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("Table.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath path = xFactory.newXPath();
    XPathExpression exp = path.compile("/tables/table");
    NodeList nlTables = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    for (int tblIndex = 0; tblIndex < nlTables.getLength(); tblIndex++) {
        Node table = nlTables.item(tblIndex);
        Node nAtt = table.getAttributes().getNamedItem("title");
        System.out.println(nAtt.getTextContent());
        exp = path.compile("headings/heading");
        NodeList nlHeaders = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
        Set<String> headers = new HashSet<String>(25);
        for (int index = 0; index < nlHeaders.getLength(); index++) {
            headers.add(nlHeaders.item(index).getTextContent().trim());
        }/*  w w w .j a  v  a 2s .c  o m*/
        for (String header : headers) {
            System.out.println(header);
        }
        exp = path.compile("tablebody/tablerow");
        NodeList nlRows = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
        for (int index = 0; index < nlRows.getLength(); index++) {
            Node rowNode = nlRows.item(index);
            exp = path.compile("tablecell/item");
            NodeList nlValues = (NodeList) exp.evaluate(rowNode, XPathConstants.NODESET);
            List<String> values = new ArrayList<String>(25);
            for (int valueIndex = 0; valueIndex < nlValues.getLength(); valueIndex++) {
                values.add(nlValues.item(valueIndex).getTextContent().trim());
            }
            for (String value : values) {
                System.out.println(value);
            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException, SAXException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document dom = parser.getDocument();

    NodeList games = dom.getElementsByTagName("game");

    for (int i = 0; i < games.getLength(); i++) {
        Node aNode = games.item(i);
        System.out.println(aNode.getFirstChild().getNodeValue());

        NamedNodeMap attributes = aNode.getAttributes();

        for (int a = 0; a < attributes.getLength(); a++) {
            Node theAttribute = attributes.item(a);
            System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
        }//from   w  w  w.  jav  a2s.c o m
    }

}

From source file:Main.java

public static String getId(Node node) {
    return node.getAttributes().getNamedItem("id").getNodeValue();
}

From source file:Main.java

public static String getAttribute(String aAttrName, Node aNode) {
    return aNode.getAttributes().getNamedItem(aAttrName).getNodeValue();
}

From source file:Main.java

public static boolean hasAttr(Node node, String name) {
    return node.getAttributes().getNamedItem(name) != null;
}

From source file:Main.java

public static String getAttr(Node node, String name) {
    return node.getAttributes().getNamedItem(name).getNodeValue();
}

From source file:Main.java

public static Node getAttribute(Node n, String name) {
    return n.getAttributes().getNamedItem(name);
}

From source file:Main.java

public static String getAttribute(Node n, String attrName) {
    if (n.getAttributes().getNamedItem(attrName) != null) {
        return n.getAttributes().getNamedItem(attrName).getNodeValue();
    }/*from  w  w w. j  av  a2  s  . c o m*/
    return "";
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    if (node.getAttributes().getLength() > 0) {
        if (node.getAttributes().getNamedItem(name) != null)
            return (node.getAttributes().getNamedItem(name).getNodeValue());
    }/* w w  w .  ja va2s.com*/
    return "";
}