Example usage for org.w3c.dom Element getNodeName

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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 {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new File("D:/test.xml"));

    NodeList elt = doc.getElementsByTagName("EMPLOYEE");
    for (int k = 0; k < elt.getLength(); k++) {
        Node firstNode3 = elt.item(k);
        Element elt1 = (Element) firstNode3;
        String att = elt1.getAttribute("PERMANENT");
        System.out.println("\n\nPERMANENT: " + att);

        NodeList nodes = elt1.getElementsByTagName("DETAILS");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childNode = nodes.item(i);
            Element elt2 = (Element) childNode;
            System.out.println("---" + elt2.getNodeName());
            System.out.println("NAME:" + elt2.getAttribute("NAME"));
            System.out.println("ID:" + elt2.getAttribute("ID"));
            System.out.println("AGE:" + elt2.getAttribute("AGE"));
        }//from   w ww .ja v  a2  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());
        }/*w w w . j  a  v a 2  s .  c o  m*/
    }
    System.out.println(stringBuilder);
}

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());
            }/*from www. ja  v a 2 s . c  o  m*/
        }
    }
}

From source file:Main.java

public static String getElementName(Element element) {
    return element.getNodeName();
}

From source file:Main.java

private static boolean isPlatformElement(Element element) {
    String elementName = element.getNodeName();
    return elementName.equals(TAG_PLATFORM);
}

From source file:Main.java

public static List<String> getByName(String name, Element node) {
    List<String> r = new ArrayList<String>();
    List<Element> l = getRealChilds(node);
    for (Element n : l) {
        if (n.getNodeName().equals(name)) {
            r.add(n.getTextContent());/*  www .j ava2 s  .c  o m*/
        }
    }
    return r;
}

From source file:Main.java

/**
 * Check if an element describes a computed goal or not.
 * /* w w w . j  av  a2 s. co  m*/
 * @param element the goal element
 * @return if the element represents a computed goal
 */
public static boolean isComputedGoal(final Element element) {
    return "computedGoal".equals(element.getNodeName());
}

From source file:Main.java

/**
 * This method checks whether the given element node has a child element with the given name.
 * @param node - parent to check//from  w w  w.  j a va2  s  .  c o m
 * @param name - name of child
 * @return - true if parent has a child with the given name, false otherwise
 */
public static boolean hasChild(Element node, String name) {
    List<Element> children = getDirectChildElementsByTag(node, DOM_WILDCARD);
    for (Element e : children) {
        if (e.getNodeName().equals(name)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " begin...");
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);/*  ww  w . j  a  va  2s .  co m*/
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);
            logger.debug("remove child '" + nd + "' success.");
        }
    }
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " end.");
}

From source file:Main.java

/**
 * Trys to find a child element in the given parent element.
 *
 * @param parent The Element to search in.
 * @param name   The name of the element to search for.
 *
 * @return The Element if found, null otherwise.
 *///from   w  w w .ja  va  2 s  .  c o m
public static Element findElement(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n.getNodeType() == n.ELEMENT_NODE) {
            Element e = (Element) n;
            if (e.getNodeName().equals(name)) {
                return e;
            }
        }
    }
    return null;
}