List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:com.weibo.wesync.notify.xml.DomUtils.java
/** * Extract the text value from the given DOM element, ignoring XML comments. <p>Appends all CharacterData nodes and * EntityReference nodes into a single String value, excluding Comment nodes. * * @see CharacterData/*from w w w .jav a 2 s .com*/ * @see EntityReference * @see Comment */ public static String getTextValue(Element valueEle) { Assert.notNull(valueEle, "Element must not be null"); StringBuilder sb = new StringBuilder(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i); if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { sb.append(item.getNodeValue()); } } return sb.toString(); }
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 a 2s . c o m 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
public static Element getFirstChildElementNS(Element elm, String tns, String localName) { if (tns == null && localName == null) return getFirstChildElement(elm); if (tns == null || tns.length() == 0) return getFirstChildElement(elm, localName); NodeList nl = elm.getChildNodes(); for (int c = 0; c < nl.getLength(); c++) { Node node = nl.item(c);/* ww w.j av a 2s. c o m*/ if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (localName == null && tns.equals(node.getNamespaceURI())) return (Element) node; if (localName != null && tns.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName())) return (Element) node; } return null; }
From source file:Main.java
/** * Find element.// www. j a va 2 s . co m * * @param topElm * the top elm * @param localName * the local name * @param namespace * the namespace * @return the element */ public static Element findElement(Element topElm, String localName, String namespace) { Stack<Element> stack = new Stack<Element>(); stack.push(topElm); while (!stack.isEmpty()) { Element curElm = stack.pop(); if ((curElm.getLocalName().equals(localName)) && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) { return curElm; } NodeList childNodes = curElm.getChildNodes(); for (int i = 0, ll = childNodes.getLength(); i < ll; i++) { Node item = childNodes.item(i); if (item.getNodeType() == Node.ELEMENT_NODE) { stack.push((Element) item); } } } return null; }
From source file:Main.java
/** * Returns an iterator over the children of the given element with * the given tag name.// w w w . j a v a 2 s . c o m * * @param element The parent element * @param tagName The name of the desired child * @return An interator of children or null if element is null. */ public static Iterator getChildrenByTagName(Element element, String tagName) { if (element == null) return null; // getElementsByTagName gives the corresponding elements in the whole // descendance. We want only children NodeList children = element.getChildNodes(); ArrayList goodChildren = new ArrayList(); for (int i = 0; i < children.getLength(); i++) { Node currentChild = children.item(i); if (currentChild.getNodeType() == Node.ELEMENT_NODE && ((Element) currentChild).getTagName().equals(tagName)) { goodChildren.add((Element) currentChild); } } return goodChildren.iterator(); }
From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java
/** * @param resourceName The XML file containing the path configuration * @return The paths defined in the XML file * @throws ParserConfigurationException//from www .j a v a 2 s. c om * @throws IOException * @throws SAXException * @throws URISyntaxException */ // TODO: Clean up this exception-mess. Something like "InitialisationException" or "ConfigurationErrorException" should cover all these exceptions. public static CliMigrationPaths initialiseFromFile(String resourceName) throws ParserConfigurationException, IOException, SAXException, URISyntaxException { InputStream instream; ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL resourceURL = loader.getResource(resourceName); if (new File(defaultPath, resourceName).isFile()) { instream = new FileInputStream(new File(defaultPath, resourceName)); } else if (new File(resourceName).isFile()) { instream = new FileInputStream(new File(resourceName)); } else if (resourceURL != null) { instream = resourceURL.openStream(); } else { String msg = String.format("Could not locate resource %s", resourceName); throw new FileNotFoundException(msg); } CliMigrationPaths paths = new CliMigrationPaths(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse(instream); Element fileformats = doc.getDocumentElement(); if (fileformats != null) { NodeList children = fileformats.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals("path")) { CliMigrationPath pathdef = decodePathNode(child); paths.add(pathdef); } } } } IOUtils.closeQuietly(instream); return paths; }
From source file:Main.java
public static void mergeElement(Element from, Element to) { // attrs// w w w . jav a2s . co m for (int idx = 0; idx < from.getAttributes().getLength(); idx++) { Node node = from.getAttributes().item(idx); to.getAttributes().setNamedItem(node.cloneNode(false)); } // children for (int idx = 0; idx < from.getChildNodes().getLength(); idx++) { Node node = from.getChildNodes().item(idx); if (!Element.class.isInstance(node)) { continue; } to.appendChild(node.cloneNode(true)); } }
From source file:com.omertron.traileraddictapi.tools.DOMHelper.java
/** * Gets the string value of the tag element name passed * * @param element/*from ww w .ja v a 2 s. co m*/ * @param tagName * @return */ public static String getValueFromElement(Element element, String tagName) { NodeList elementNodeList = element.getElementsByTagName(tagName); String value = ""; if (elementNodeList != null) { Element tagElement = (Element) elementNodeList.item(0); if (tagElement != null) { NodeList tagNodeList = tagElement.getChildNodes(); if (tagNodeList != null && tagNodeList.getLength() > 0) { value = ((Node) tagNodeList.item(0)).getNodeValue(); } } } return value; }
From source file:no.kantega.commons.util.XMLHelper.java
public static String getText(Element element) { StringBuilder buffer = new StringBuilder(); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Text) { buffer.append(((Text) children.item(i)).getData()); }// ww w. ja v a 2 s .c o m } return buffer.toString(); }
From source file:Main.java
/** * Returns the children elements with the specified tagName for the * specified parent element.//from w ww . j av a 2s . c om * * @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; }