List of usage examples for org.w3c.dom Element getNodeName
public String getNodeName();
From source file:Main.java
public static Map<String, String> getChildTextMap(Element elt) { Map<String, String> data = new HashMap<String, String>(); NodeList nodes = elt.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) child; data.put(e.getNodeName(), getInnerText(e)); }// w w w. j av a2 s. c o m } return data; }
From source file:Main.java
public static Element cloneElementAs(Element srcEl, Document dstDoc, String elName) { if (srcEl.getNodeName().equals(elName)) { if (srcEl.getOwnerDocument() == dstDoc) { return (Element) srcEl.cloneNode(true); } else {// w w w . j a va 2s .c om return (Element) dstDoc.importNode(srcEl, true); } } else { final Element dstEl = dstDoc.createElement(elName); final NodeList srcChildren = srcEl.getChildNodes(); final int n = srcChildren.getLength(); for (int i = 0; i < n; ++i) { final Node srcChild = srcChildren.item(i); final Node dstChild = dstDoc.importNode(srcChild, true); dstEl.appendChild(dstChild); } return dstEl; } }
From source file:Main.java
/** * Dumps a debug listing of the attributes of the given element to * System.out./*w w w .j a v a2 s . com*/ * * @param elem the element to dump the attributes of */ public static void dumpAttrs(Element elem) { System.out.println("Attributes of " + elem.getNodeName() + ", NS: " + elem.getNamespaceURI()); NamedNodeMap attrs = elem.getAttributes(); int len = attrs.getLength(); for (int i = 0; i < len; ++i) { Node attr = attrs.item(i); System.out.println(" Name: " + attr.getNodeName() + ", Value: " + attr.getNodeValue() + ", NS: " + attr.getNamespaceURI()); } }
From source file:Main.java
/** * Rename an element, replacing it in its document. * @param element the element to rename. * @param name the new element name.// w w w . j a v a2 s . c om * @return the renamed element. */ public static Element renameElement(Element element, String name) { if (element.getNodeName().equals(name)) return element; Element el = element.getOwnerDocument().createElement(name); //Copy the attributes NamedNodeMap attributes = element.getAttributes(); int nAttrs = attributes.getLength(); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); el.setAttribute(attr.getNodeName(), attr.getNodeValue()); } //Copy the children Node node = element.getFirstChild(); while (node != null) { Node clone = node.cloneNode(true); el.appendChild(clone); node = node.getNextSibling(); } //Replace the element element.getParentNode().replaceChild(el, element); return el; }
From source file:Main.java
static void printElement(Element element, String indent) { System.out.println("Element '" + element.getNodeName() + "'"); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); switch (child.getNodeType()) { case Node.ELEMENT_NODE: printElement((Element) child, indent + "\t"); break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr) child; System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'"); break; case Node.COMMENT_NODE: Comment comment = (Comment) child; System.out.println("\tComment: '" + comment.getData() + "'"); break; case Node.CDATA_SECTION_NODE: CharacterData cdata = (CharacterData) child; System.out.println("\tCDatat: '" + cdata.getData() + "'"); break; case Node.TEXT_NODE: Text text = (Text) child; System.out.println("\tText: '" + text.getData() + "'"); break; default:/*w w w. ja va2 s . c om*/ System.out.println("\tUnknown node type: '" + child.getNodeType() + "'"); break; } } }
From source file:Main.java
/** * Returns the children elements with the specified tagName for the * specified parent element.//from www. j a v a 2s. com * * @param parent The parent whose children we're looking for. * @param tagName the name of the child to find * @return List of the children with the specified name * @throws NullPointerException if parent or tagName are null */ public static List<Element> findChildren(Element parent, String tagName) { if (parent == null || tagName == null) throw new NullPointerException( "Parent or tagname were null! " + "parent = " + parent + "; tagName = " + tagName); List<Element> result = new ArrayList<Element>(); NodeList nodes = parent.getChildNodes(); Node node; int len = nodes.getLength(); for (int i = 0; i < len; i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.getNodeName().equals(tagName)) result.add(element); } } return result; }
From source file:Main.java
/** * Finds and returns the first child node with the given name and * attribute name, value pair.//from w w w .ja v a 2 s. com */ public static Element getFirstChildElement(Node parent, String elemName, String attrName, String attrValue) { // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) child; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } child = child.getNextSibling(); } // not found return null; }
From source file:Main.java
/** * Finds and returns the last child node with the given name and * attribute name, value pair.//from w w w. j a va2 s. c o m */ public static Element getLastChildElement(Node parent, String elemName, String attrName, String attrValue) { // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) child; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
public static List getChildElements(Element parent, String childName) { NodeList children = parent.getChildNodes(); List list = new ArrayList(); int size = children.getLength(); for (int i = 0; i < size; i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (childName.equals(element.getNodeName())) { list.add(element);/* w w w.ja v a 2 s .c om*/ } } } return list; }
From source file:Main.java
/** * Finds and returns the next sibling node with the given name and * attribute name, value pair. Since only elements have attributes, * the node returned will be of type Node.ELEMENT_NODE. */// w ww . j av a 2 s.c o m public static Element getNextSiblingElement(Node node, String elemName, String attrName, String attrValue) { // search for node Node sibling = node.getNextSibling(); while (sibling != null) { if (sibling.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) sibling; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } sibling = sibling.getNextSibling(); } // not found return null; }