List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.java
public static ArrayList<Element> getChildElements(Element parent, String childName) { ArrayList<Element> childElements = new ArrayList<Element>(); NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (childName == null || childNode.getNodeName().equals(childName)) { childElements.add((Element) childNode); }/* w w w .j a v a2 s . c o m*/ } } return childElements; }
From source file:Main.java
/*************************************************************************** * Sets the value of the first child under the given element with the given * name. If the child has no nodes, one is created. * //from www. j av a 2s . c om * @param doc * @param e * @param name * @param value * @return * @throws Exception **************************************************************************/ public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception { NodeList childNodes = e.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (node.getNodeName().equals(name)) { Node child = node.getFirstChild(); if (child == null) ((Element) node).appendChild(doc.createTextNode(value)); else child.setNodeValue(value); return true; } } return false; }
From source file:Main.java
public static boolean isNodeTypeElement(Node node, String expectedElementName) { return isNodeTypeElement(node) && (expectedElementName == null || expectedElementName.equals(node.getNodeName())); }
From source file:Main.java
public static void ReadXMLFile() { try {/*from w w w . j a va 2 s .c om*/ File fXmlFile = new File("D:\\FAR_Documents\\__Startamap\\Home.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("staff"); System.out.println("----------------------------"); 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( "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()); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String getXMLValueByName(String xmlFilePath, String name) { String returnValue = "No Value!"; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try {//w w w. j a va 2s .c om DocumentBuilder builder = builderFactory.newDocumentBuilder(); InputStream is = new FileInputStream(xmlFilePath); Document doc = builder.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(name)) { returnValue = node.getTextContent().trim(); } } } } } catch (Exception e) { } return returnValue; }
From source file:Main.java
public static ArrayList<Node> getNodeList(Node parentNode, String nodeName, Map<String, String> attributesMap) { ArrayList<Node> returnNodeList = new ArrayList<Node>(); try {//from w w w . j a va 2 s .c om DocumentTraversal dt = (DocumentTraversal) parentNode.getOwnerDocument(); NodeIterator i = dt.createNodeIterator(parentNode, NodeFilter.SHOW_ELEMENT, null, false); Node node = i.nextNode(); while (node != null) { if (node.getNodeName().equals(nodeName)) { if (attributesExist(node, attributesMap)) { returnNodeList.add(node); } } node = i.nextNode(); } } catch (Exception ex) { logger.error(ex); } return returnNodeList; }
From source file:Main.java
public static List<Node> getNodeListByName(Node node, String name, List<Node> finalNodeList) { NodeList nodeList = node.getChildNodes(); System.out.println("child node name: " + node.getNodeName()); if (nodeList == null) return null; for (int i = 0; i < nodeList.getLength(); i++) { Node candNode = nodeList.item(i); if (candNode.getNodeName().equals(name)) { finalNodeList.add(candNode); }//from w w w. j a v a 2s . co m getNodeListByName(nodeList.item(i), name, finalNodeList); } return finalNodeList; }
From source file:DomUtils.java
/** * Namespace-aware equals comparison. Returns <code>true</code> if either * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>, * otherwise returns <code>false</code>. *//* w ww .ja v a 2s .c o m*/ public static boolean nodeNameEquals(Node node, String desiredName) { return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName())); }
From source file:Main.java
/** * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name. * @param xnode the node to get the child node from * @param name the name of the child node to search for * @param caseSensitive boolean true for case sensitive name matching, false otherwise * @return the located node or null if one was not found *//*from w ww . j a va2 s . c o m*/ public static Node getChildNodeByName(final Node xnode, final String name, final boolean caseSensitive) { NodeList list = xnode.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (caseSensitive) { if (node.getNodeName().equals(name)) return node; } else { if (node.getNodeName().equalsIgnoreCase(name)) return node; } } return null; }
From source file:Main.java
private static void walkNodes(Node nodeIn, HashSet<String> hElements) { if (nodeIn == null) return;/*from w ww . j a v a 2s . c o m*/ NodeList nodes = nodeIn.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { String sNodeName = n.getNodeName(); if (!hElements.contains(sNodeName)) hElements.add(sNodeName); walkNodes(n, hElements); } } }