List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.java
/** * Convert an XML fragment from one namespace to another. * * @param from element to translate * @param namespace namespace to be translated to * @return the element in the new namespace * * @since 8.4/*ww w . j a va 2 s . co m*/ */ public static Element translateXML(Element from, String namespace) { Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode; if (node.getNodeType() == Node.ELEMENT_NODE) { newNode = translateXML((Element) node, namespace); } else { newNode = node.cloneNode(true); } to.appendChild(newNode); } NamedNodeMap m = from.getAttributes(); for (int i = 0; i < m.getLength(); i++) { Node attr = m.item(i); to.setAttribute(attr.getNodeName(), attr.getNodeValue()); } return to; }
From source file:Main.java
/** * Extracts from the given Element the first child having the given name. * * @param element/* w ww . j a v a 2s . c om*/ * @param name * @return the first child with the given name or null if none */ public static Element firstChild(Element element, String name) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node current = nodeList.item(i); if (current.getNodeType() == Node.ELEMENT_NODE && current.getNodeName().equals(name)) { return (org.w3c.dom.Element) current; } } return null; }
From source file:Main.java
/** * Are elements equal.//from w ww . j a v a 2s . c om * * @param element1 * the element1 * @param element2 * the element2 * @return true, if successful */ public static boolean areElementsEqual(Element element1, Element element2) { if (!element1.getTagName().equals(element2.getTagName())) { return false; } NamedNodeMap nodeAttrMap = element1.getAttributes(); NamedNodeMap pathAttrMap = element2.getAttributes(); if ((nodeAttrMap == null && pathAttrMap == null) || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) { return true; } else { if (element1.hasAttribute("name") && element2.hasAttribute("name")) { if (element1.getAttribute("name").equals(element2.getAttribute("name"))) { return true; } } else if (nodeAttrMap != null && pathAttrMap != null && (nodeAttrMap.getLength() == pathAttrMap.getLength())) { for (int k = 0; k < nodeAttrMap.getLength(); k++) { Node nodeAttr = nodeAttrMap.item(k); String nodeAttrName = nodeAttr.getNodeName(); String nodeAttrValue = nodeAttr.getNodeValue(); if (element2.hasAttribute(nodeAttrName) && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) { return true; } } } } return false; }
From source file:Main.java
static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws Exception { ArrayList<Element> resultVector = new ArrayList<Element>(); if (element == null) { return resultVector; }//from w w w .j a v a2 s. c om if (xpathExpression.indexOf("/") == -1) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) { resultVector.add((Element) node); } } } else { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); resultVector.add((Element) node); } } return resultVector; }
From source file:Main.java
/** * @param node//ww w . j ava 2 s . co m * @param name * @return a map with all children nodes with the given name * mapped by the value of their first child */ public static Map<String, Node> findChildrenMapByFirstChild(Node node, String name) { Map<String, Node> ret = new HashMap<String, Node>(); NodeList nl = node.getChildNodes(); int len = nl.getLength(); Node child; for (int i = 0; i < len; i++) { child = nl.item(i); if (name.equals(child.getNodeName())) { ret.put(child.getFirstChild().getNodeValue(), child); } } return ret; }
From source file:Main.java
public static int indexOf(NodeList nodeList, String tagName, int startIdx) { for (int i = startIdx; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) { if (node.getNodeName().equals(tagName)) { return i; }/*from w w w . j a v a2 s . co m*/ } } return -1; }
From source file:Main.java
public static void nodeSubUpdate(Element root, String table, String queryType, String queryValue) { Node find = getTag(root, table); Document doc = find.getOwnerDocument(); NodeList nl = find.getChildNodes(); int numnodes = nl.getLength(); System.out.println("length : " + numnodes); Node replNode = null;/*from w w w.j a v a2 s . c o m*/ for (int i = 0; i < numnodes; i++) { Node n = nl.item(i); String name = n.getNodeName(); if (name.equals(queryType)) { replNode = n; System.out.println("Finding Node : " + replNode.getNodeName()); break; } } Element type = doc.createElement(queryType); Text value = doc.createTextNode(queryValue); type.appendChild(value); find.replaceChild(type, replNode); print(doc); }
From source file:Main.java
public static String GetChildElementText(Element element, String name) { if (element != null && name != null && !name.isEmpty()) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && name.equals(node.getNodeName())) { return node.getTextContent(); }/*from w w w .j ava 2s . c o m*/ } } return null; }
From source file:Main.java
public static String GetChildElementText(Element element, String name, String defaultValue) { if (element != null && name != null && !name.isEmpty()) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && name.equals(node.getNodeName())) { return node.getTextContent(); }/*from w w w. j a v a2 s . c o m*/ } } return defaultValue; }
From source file:Main.java
/** Helper method - gets the local name of the node without the namespace prefix. The method checks to be sure that the DOM 2 interface is implemented otherwise an exception is thrown when the method is called. @param pNode A <I>org.w3c.dom.Node</I> object. @return the local name./* w ww.j a va 2 s . com*/ */ public static String getLocalName(Node pNode) { try { return pNode.getLocalName(); } catch (NoSuchMethodError pEx) { return pNode.getNodeName(); } }