List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** * Find all direct child elements of an element. Children which are * all-whitespace text nodes or comments are ignored; others cause an * exception to be thrown.//from w w w . jav a2 s. c om * * @param parent a parent element in a DOM tree * @return a list of direct child elements (may be empty) * @throws IllegalArgumentException if there are non-element children * besides whitespace * * @since 8.4 */ public static List<Element> findSubElements(Element parent) throws IllegalArgumentException { NodeList l = parent.getChildNodes(); List<Element> elements = new ArrayList<Element>(l.getLength()); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) n); } else if (n.getNodeType() == Node.TEXT_NODE) { String text = ((Text) n).getNodeValue(); if (text.trim().length() > 0) { throw new IllegalArgumentException("non-ws text encountered in " + parent + ": " + text); // NOI18N } } else if (n.getNodeType() == Node.COMMENT_NODE) { // OK, ignore } else { throw new IllegalArgumentException("unexpected non-element child of " + parent + ": " + n); // NOI18N } } return elements; }
From source file:DOMEdit.java
public static void newEmail(Document doc, String newname, String newemail) { Element root = doc.getDocumentElement(); NodeList rootlist = root.getChildNodes(); for (int i = 0; i < rootlist.getLength(); i++) { Element person = (Element) rootlist.item(i); NodeList personlist = person.getChildNodes(); Element name = (Element) personlist.item(0); NodeList namelist = name.getChildNodes(); Text nametext = (Text) namelist.item(0); String oldname = nametext.getData(); if (oldname.equals(newname)) { Element email = (Element) personlist.item(1); NodeList emaillist = email.getChildNodes(); Text emailtext = (Text) emaillist.item(0); emailtext.setData(newemail); }//from w w w . j a v a 2s .c o m } }
From source file:Main.java
/** * Get all direct child elements with a given name, for a parent element. * @param e/*from w w w .ja v a2 s . co m*/ * @return */ public static List<Element> getChildElements(Element e, String name) { if (e == null) return null; List<Element> result = new LinkedList<Element>(); NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n instanceof Element) { if (name == null || name.equals(n.getLocalName()) || n.getLocalName().endsWith(":" + name)) { result.add((Element) n); } } } return result; }
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 www. j a v a 2 s . c o m*/ } } return elementList; }
From source file:Main.java
/** * Get all direct child elements with a given name, for a parent element. * @param e// w w w. j a v a 2 s . c o m * @return */ public static List<Element> getChildElements(Element e, String name) { if (e == null) return null; List<Element> result = new LinkedList<Element>(); NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n instanceof Element) { if (name == null || name.equals(((Element) n).getLocalName()) || ((Element) n).getLocalName().endsWith(":" + name)) { result.add((Element) n); } } } return result; }
From source file:Main.java
private static Node getChildNodeByType(Element element, short nodeType) { if (element == null) return null; NodeList nodes = element.getChildNodes(); if (nodes == null || nodes.getLength() < 1) return null; Node node;/*from w ww . j av a2 s. c om*/ String data; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); short type = node.getNodeType(); if (type == nodeType) { if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { data = ((Text) node).getData(); if (data == null || data.trim().length() < 1) continue; } return node; } } return null; }
From source file:Main.java
/** * Copy elements from one document to another attaching at the specified * element and translating the namespace. * * @param from copy the children of this element (exclusive) * @param to where to attach the copied elements * @param newNamespace destination namespace * * @since 8.4/* ww w . j av a2 s . c o m*/ */ public static void copyDocument(Element from, Element to, String newNamespace) { Document doc = to.getOwnerDocument(); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { Element oldElement = (Element) node; newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); } copyDocument(oldElement, newElement, newNamespace); } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); } if (newNode != null) { to.appendChild(newNode); } } }
From source file:Main.java
private final static String getElement(String filePath, String key) throws Exception { String value = null;//from ww w . ja v a2 s. co m try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); File xmlFile = new File(filePath); Document document = documentBuilder.parse(xmlFile); Element resourcesElement = document.getDocumentElement(); resourcesElement.normalize(); NodeList strings = resourcesElement.getChildNodes(); int stringsCount = strings.getLength(); for (int i = 0; i < stringsCount; i++) { Node node = strings.item(i); short nodeType = node.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { Element element = (Element) node; String attribute = element.getAttribute("name"); if (attribute.equals(key)) { value = element.getTextContent(); } } } } catch (Exception ex) { throw ex; } return value; }
From source file:Main.java
public static Element getChild(Element node, String tagName) { Element element = null;/*from w w w . j a va 2s. co m*/ do { if (node == null) { break; } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(tagName)) { element = (Element) child; break; } } } } while (false); return element; }
From source file:Main.java
/** * Get the content of the given element. * * @param element The element to get the content for. * @param defaultStr The default to return when there is no content. * @return The content of the element or the default. *//* www.j av a2 s . c o m*/ public static String getElementContent(Element element, String defaultStr) throws Exception { if (element == null) { return defaultStr; } NodeList children = element.getChildNodes(); StringBuilder result = new StringBuilder(""); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) { result.append(children.item(i).getNodeValue()); } // else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) { // // Ignore comment nodes // } } return result.toString().trim(); }