List of usage examples for org.w3c.dom Element getNodeName
public String getNodeName();
From source file:Main.java
private static List<Element> getChildElementsByName(Element parentElement, String childElementName) { List<Element> elementList = new ArrayList<Element>(); NodeList childNodes = parentElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) node; if (childElement.getNodeName().equals(childElementName)) { elementList.add(childElement); }/*from w w w. java 2s .c o m*/ } } return elementList; }
From source file:Main.java
/** * @return A String[] of length two, [prefix, URI]. *///from w w w .j a v a2 s . c o m public static String[] getNamespaceDeclaration(Element ele) { String prefixHint = null; String[] parts = ele.getNodeName().split(":"); if (parts.length == 2) { prefixHint = parts[0]; } return getNamespaceDeclaration(ele, prefixHint); }
From source file:Main.java
/** * Find a child element.// ww w . j a va 2s. c om * @param e The element to search. * @param find The name to search for. * @return The element found, or null if not found. */ public static Element findElement(final Element e, final String find) { for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } final Element el = (Element) child; if (el.getNodeName().equals(find)) { return el; } } return null; }
From source file:Main.java
static public void fillHashtable(NodeList list, Hashtable<String, String> fillIn) { for (int curElemNum = 0; curElemNum < list.getLength(); curElemNum++) { if (list.item(curElemNum).getNodeType() != Node.ELEMENT_NODE) continue; Element curValue = (Element) list.item(curElemNum); String valueName = curValue.getNodeName(); String value = curValue.getFirstChild().getNodeValue(); fillIn.put(valueName, value);//from w w w . ja v a 2 s .c o m } }
From source file:Main.java
public static void getNamedChildren(Element e, String name, List<Element> set) { Element c = getFirstChild(e); while (c != null) { if (name.equals(c.getLocalName()) || name.equals(c.getNodeName())) set.add(c);/* ww w . ja v a 2 s .co m*/ c = getNextSibling(c); } }
From source file:Main.java
public static Element getElementByTagNameNS(Node element, String namespace, String name) { NodeList elements = element.getChildNodes(); CharSequence colon = ":"; if (elements != null) { for (int i = 0; i < elements.getLength(); i++) { if (elements.item(i).getNodeType() == Node.ELEMENT_NODE && (elements.item(i).getAttributes() .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == null || !"true".equals(elements.item(i).getAttributes() .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil")))) { Element currentElement = (Element) elements.item(i); String nodeName = currentElement.getNodeName(); String nodeNameOnly = nodeName; if (nodeName.contains(colon)) { String[] nodeNameSplit = nodeName.split(":"); nodeNameOnly = nodeNameSplit[1]; }// w w w . ja v a2 s . c o m if ((currentElement.getNamespaceURI() == null || currentElement.getNamespaceURI().equals(namespace)) && nodeNameOnly.equals(name)) { return currentElement; } } } } return null; }
From source file:Main.java
public static void getNamedChildrenWithWildcard(Element focus, String name, List<Element> children) { Element c = getFirstChild(focus); while (c != null) { String n = c.getLocalName() != null ? c.getLocalName() : c.getNodeName(); if (name.equals(n) || (name.endsWith("[x]") && n.startsWith(name.substring(0, name.length() - 3)))) children.add(c);//from ww w . java2 s.co m c = getNextSibling(c); } }
From source file:Main.java
public static String elementToString(Element elt, String prefix) { String str = prefix + "<" + elt.getNodeName(); // XERCES // NamedNodeMap nnm = elt.getAttributes(); // for (int i = 0; i < nnm.getLength(); i++) { // str += " " + nnm.item(i).getNodeName() + "=\"" + nnm.item(i).getNodeValue() + "\""; // }/* w ww. j a va2s . c o m*/ // // boolean hasSubElt = false; // NodeList nodeList = elt.getChildNodes(); // String str2 = ""; // for (int i = 0; i < nodeList.getLength(); i++) { // Node cur = nodeList.item(i); // if (cur.getNodeType() == Node.ELEMENT_NODE) { // str2 += elementToString((Element) cur, prefix + " "); // hasSubElt = true; // } else if (cur.getNodeType() == Node.CDATA_SECTION_NODE) { // str2 += prefix + " " + generateCDataSection(cur.getTextContent()); // hasSubElt = true; // } // } // // if (hasSubElt) { // str += ">\r\n" + str2 + prefix + "</" + elt.getNodeName() + ">\r\n"; // } else { // str2 = elt.getTextContent(); // if (str2.equals("")) { // str += "/>\r\n"; // } else { // str += ">" + str2 + "</" + elt.getNodeName() + ">\r\n"; // } // } return str; }
From source file:Main.java
public static Element getElement(Element item, String tag) { NodeList nodes = item.getElementsByTagName(tag); if (nodes != null && nodes.getLength() > 0) { Element element = (Element) nodes.item(0); String nodeName = null;// ww w . jav a2 s . co m if (element != null) { nodeName = element.getNodeName(); } if (nodeName != null && nodeName.compareTo(tag) == 0) { return element; } } return null; }
From source file:Main.java
/** * Returns the index of an element node from a provided list * @param actAllNodes//from ww w .jav a 2 s .co m * @param element * @param exceptionList - a list of indexes that will not be taken in count * @return -1 if node not found in the list */ protected static int isNodePresent(NodeList actAllNodes, Element element, ArrayList<Integer> exceptionList) { int idx = -1; int tmpIdx = -1; try { String tmpElementName = element.getNodeName(); for (int i = 0; i < actAllNodes.getLength(); i++) { if (actAllNodes.item(i).getNodeName().equals(tmpElementName)) { tmpIdx = i; if (exceptionList.indexOf(tmpIdx) == -1) { idx = tmpIdx; break; } } } return idx; } catch (Exception e) { e.printStackTrace(); return -1; } }