Example usage for org.w3c.dom Node getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xPath = XPathFactory.newInstance().newXPath();
    FileInputStream file = new FileInputStream(new File("c:/data.xml"));

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    Document xmlDocument = builder.parse(file);

    XPathExpression expr = xPath.compile("//project/*");
    NodeList list = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        System.out.println(node.getNodeName() + "=" + node.getTextContent());
    }/*from  w ww.ja va  2 s .  co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> names = new ArrayList<>();
    URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265");
    InputStream is = oracle.openStream();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);//from w  ww.j a  v a2 s. c o m
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(is);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//*:*/@*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nl = (NodeList) result;
    for (int i = 0; i < nl.getLength(); i++) {
        names.add(nl.item(i).getNodeName());
        Node node = nl.item(i);
        String path = "." + node.getNodeName() + " = " + node.getNodeValue();
        node = ((Attr) node).getOwnerElement();
        while (node != null) {
            path = node.getNodeName() + '/' + path;
            node = node.getParentNode();
        }
        System.out.println(path);
    }
}

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());
        }//ww w  .j a  va  2  s . c o m
    }

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc1 = builder.parse(fileInputStream);
    doc1.getDocumentElement().normalize();
    NodeList kList1 = doc1.getElementsByTagName("item");

    StringBuilder stringBuilder = new StringBuilder();

    for (int temp = 0; temp < kList1.getLength(); temp++) {
        Node kNode1 = kList1.item(temp);
        System.out.println("\nCurrent Element :" + kNode1.getNodeName());
        if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) kNode1;
            System.out.println("node name" + eElement.getNodeName());
            Node in = eElement.getFirstChild();
            if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty()
                    && !(in.getTextContent().length() == 0))
                stringBuilder.append(in.getTextContent());
        }/* ww w .  j  a  v a2s .  c  om*/
    }
    System.out.println(stringBuilder);
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    File fXmlFile = new File("data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("staff");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("Staff id : " + eElement.getAttribute("id"));
            System.out.println(//from  w ww  .j ava2  s.com
                    "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println(
                    "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println(
                    "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
        }
    }
}

From source file:DemoTreeWalker.java

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

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

    Document doc = parser.getDocument();

    DocumentTraversal docTraversal = (DocumentTraversal) doc;

    TreeWalker iter = docTraversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, false);
    Node n = null;
    while ((n = iter.nextNode()) != null) {
        System.out.println("Node name: " + n.getNodeName());
        System.out.println("Node value: " + n.getNodeValue());
    }/* w  ww. j  av  a  2  s.  c  o  m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml)));
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("config");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            Config c = new Config();
            c.name = eElement.getAttribute("name");
            c.type = eElement.getAttribute("type");
            c.format = eElement.getAttribute("format");
            c.size = Integer.valueOf(eElement.getAttribute("size"));
            c.scale = Integer.valueOf(eElement.getAttribute("scale"));
            String attribute = eElement.getAttribute("required");
            c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false);
            System.out.println("Imported config : " + c);
        }//from   w  w w.  ja  va2 s .c o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);/*  w  ww . j  a  v  a  2s.c o m*/
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("C:/data.xsd");

    Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0);
    System.out.println(rootNode.getNodeName());

    XPath xPath1 = XPathFactory.newInstance().newXPath();
    NamespaceContext nsContext = new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            return "http://www.w3.org/2001/XMLSchema";
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return "xs";
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            Set s = new HashSet();
            s.add("xs");
            return s.iterator();
        }
    };
    xPath1.setNamespaceContext((NamespaceContext) nsContext);
    NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET);
    System.out.println(nList1.item(0).getNodeName());

    NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET);
    System.out.println(nList2.item(0).getNodeName());
}

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("myxml.xml");
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    XPathExpression expr = xpath.compile("//" + "item1" + "/*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Element el = (Element) nodes.item(i);
        System.out.println("tag: " + el.getNodeName());
        if (el.getFirstChild().getNodeType() == Node.TEXT_NODE)
            System.out.println("inner value:" + el.getFirstChild().getNodeValue());

        NodeList children = el.getChildNodes();
        for (int k = 0; k < children.getLength(); k++) {
            Node child = children.item(k);
            if (child.getNodeType() != Node.TEXT_NODE) {
                System.out.println("child tag: " + child.getNodeName());
                if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
                    System.out.println("inner child value:" + child.getFirstChild().getNodeValue());
            }/*w  w  w  .  ja  va  2  s .  c o  m*/
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<Services><service name='qwerty' id=''><File rootProfile='abcd' extension='acd'><Columns>"
            + "<name id='0' profileName='DATE' type='java'></name><name id='1' profileName='DATE' type='java'></name>"
            + "</Columns></File><File rootProfile='efg' extension='ghi'><Columns><name id='a' profileName='DATE' type='java'></name>"
            + "<name id='b' profileName='DATE' type='java'></name></Columns></File></service></Services>";
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;

    documentBuilder = documentFactory.newDocumentBuilder();

    org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes())));

    doc.getDocumentElement().normalize();
    NodeList nodeList0 = doc.getElementsByTagName("service");
    NodeList nodeList1 = null;//from ww w . ja va 2 s  . c o m
    NodeList nodeList2 = null;
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {
        Node node0 = nodeList0.item(temp0);

        System.out.println("\nElement type :" + node0.getNodeName());
        Element Service = (Element) node0;

        if (node0.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        System.out.println("name : " + Service.getAttribute("name"));
        System.out.println("id : " + Service.getAttribute("id"));
        nodeList1 = Service.getChildNodes();
        for (int temp = 0; temp < nodeList1.getLength(); temp++) {
            Node node1 = nodeList1.item(temp);

            System.out.println("\nElement type :" + node1.getNodeName());

            Element File = (Element) node1;

            if (node1.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            System.out.println("rootProfile:" + File.getAttribute("rootProfile"));
            System.out.println("extension  : " + File.getAttribute("extension"));

            nodeList2 = File.getChildNodes();// colums
            for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {
                Element column = (Element) nodeList2.item(temp1);
                NodeList nodeList4 = column.getChildNodes();
                for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) {
                    Element name = (Element) nodeList4.item(temp3);
                    if (name.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    System.out.println("id:" + name.getAttribute("id"));
                    System.out.println("profileName  : " + name.getAttribute("profileName"));
                    System.out.println("type  : " + name.getAttribute("type"));
                }
            }
        }
    }
}