List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:XMLUtils.java
/** * Get the next sibling element of a given element. * @param el//from w ww .j av a2s . com * @return */ public static Element getNextSibling(Element el) { String tagName = el.getTagName(); if (tagName == null) { return null; } Node n = el.getNextSibling(); while (n != null && (!(n instanceof Element) || !tagName.equals(((Element) n).getTagName()))) { // get the next one n = n.getNextSibling(); } if (n instanceof Element) { return (Element) n; } else { // else, nothing to return return null; } }
From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java
private static Node findFirstDescendantHasAttr(Node parent, String attrName, String attrValue) { if (parent == null) return null; Node n = parent.getFirstChild(); while (n != null) { NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { Node attr = attrs.getNamedItem(attrName); if (attr != null) { if (attr.getNodeValue().equals(attrValue)) { return n; }//from w ww .j a v a2 s . c o m } } Node d = findFirstDescendantHasAttr(n, attrName, attrValue); if (d != null) { return d; } n = n.getNextSibling(); } return n; }
From source file:eu.semaine.util.XMLTool.java
/** * Get a list of all direct children with the given tag name and namespace. * Whereas getChildElementByTagNameNS() returns the single first child, * this method returns all the children that match. * @param node//from ww w . j av a 2s .c om * @param childName * @param childNamespace * @return a list containing the children that match, or an empty list if none match. * @throws MessageFormatException */ public static List<Element> getChildrenByTagNameNS(Node node, String childName, String childNamespace) { List<Element> list = new ArrayList<Element>(); Element e = getChildElementByTagNameNS(node, childName, childNamespace); if (e != null) { list.add(e); Node n = e; while ((n = n.getNextSibling()) != null) { if (n.getNodeType() == Node.ELEMENT_NODE && isSameNamespace(n.getNamespaceURI(), childNamespace) && n.getNodeName().equals(childName)) { list.add((Element) n); } } } return list; }
From source file:eu.semaine.util.XMLTool.java
/** * Get a list of all direct children with the given local name and namespace. * Whereas getChildElementByTagNameNS() returns the single first child, * this method returns all the children that match. * @param node/*from w ww. j a v a 2s. co m*/ * @param childName the child's local name * @param childNamespace * @return a list containing the children that match, or an empty list if none match. * @throws MessageFormatException */ public static List<Element> getChildrenByLocalNameNS(Node node, String childName, String childNamespace) { List<Element> list = new ArrayList<Element>(); Element e = getChildElementByLocalNameNS(node, childName, childNamespace); if (e != null) { list.add(e); Node n = e; while ((n = n.getNextSibling()) != null) { if (n.getNodeType() == Node.ELEMENT_NODE && isSameNamespace(n.getNamespaceURI(), childNamespace) && n.getLocalName().equals(childName)) { list.add((Element) n); } } } return list; }
From source file:com.xpn.xwiki.plugin.feed.SyndEntryDocumentSource.java
/** * Computes the sum of lengths of all the text nodes within the given DOM sub-tree * /* www. j av a 2 s. com*/ * @param node the root of the DOM sub-tree containing the text * @return the sum of lengths of text nodes within the given DOM sub-tree */ public static int innerTextLength(Node node) { switch (node.getNodeType()) { case Node.TEXT_NODE: return node.getNodeValue().length(); case Node.ELEMENT_NODE: int length = 0; Node child = node.getFirstChild(); while (child != null) { length += innerTextLength(child); child = child.getNextSibling(); } return length; case Node.DOCUMENT_NODE: return innerTextLength(((org.w3c.dom.Document) node).getDocumentElement()); default: return 0; } }
From source file:org.dataone.proto.trove.mn.http.client.HttpExceptionHandler.java
private static ErrorElements deserializeXml(HttpResponse response) throws IllegalStateException, IOException // throws NotFound, InvalidToken, ServiceFailure, NotAuthorized, // NotFound, IdentifierNotUnique, UnsupportedType, // InsufficientResources, InvalidSystemMetadata, NotImplemented, // InvalidCredentials, InvalidRequest, IOException { { ErrorElements ee = new ErrorElements(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc;/*ww w .jav a2 s . com*/ int httpCode = response.getStatusLine().getStatusCode(); if (response.getEntity() != null) { BufferedInputStream bErrorStream = new BufferedInputStream(response.getEntity().getContent()); bErrorStream.mark(5000); // good for resetting up to 5000 bytes String detailCode = null; String description = null; String name = null; int errorCode = -1; try { DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(bErrorStream); Element root = doc.getDocumentElement(); root.normalize(); if (root.getNodeName().equalsIgnoreCase("error")) { if (root.hasAttribute("errorCode")) { try { errorCode = Integer.getInteger(root.getAttribute("errorCode")); } catch (NumberFormatException nfe) { System.out.println("errorCode unexpectedly not able to parse to int," + " using http status for creating exception"); errorCode = httpCode; } } if (errorCode != httpCode) // throw new ServiceFailure("1000","errorCode in message body doesn't match httpStatus"); { System.out.println("errorCode in message body doesn't match httpStatus," + " using errorCode for creating exception"); } if (root.hasAttribute("detailCode")) { detailCode = root.getAttribute("detailCode"); } else { detailCode = "detail code is Not Set!"; } if (root.hasAttribute("name")) { name = root.getAttribute("name"); } else { name = "Exception"; } Node child = root.getFirstChild(); do { if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equalsIgnoreCase("description")) { Element element = (Element) child; description = element.getTextContent(); break; } } } while ((child = child.getNextSibling()) != null); } else { description = domToString(doc); detailCode = "detail code was never Set!"; } } catch (TransformerException e) { description = deserializeNonXMLErrorStream(bErrorStream, e); } catch (SAXException e) { description = deserializeNonXMLErrorStream(bErrorStream, e); } catch (IOException e) { description = deserializeNonXMLErrorStream(bErrorStream, e); } catch (ParserConfigurationException e) { description = deserializeNonXMLErrorStream(bErrorStream, e); } ee.setCode(errorCode); ee.setName(name); ee.setDetailCode(detailCode); ee.setDescription(description); } return ee; }
From source file:it.cnr.icar.eric.common.SOAPMessenger.java
private static org.w3c.dom.Node nextNode(org.w3c.dom.Node node) { // assert(node != null); org.w3c.dom.Node child = node.getFirstChild(); if (child != null) { return child; }//w ww. j a v a2 s .com org.w3c.dom.Node sib; while ((sib = node.getNextSibling()) == null) { node = node.getParentNode(); if (node == null) { // End of document return null; } } return sib; }
From source file:com.mediaworx.xmlutils.XmlHelper.java
/** * Removes text nodes that are empty or contain whitespace only if the parent node has at least one child of any * of the following types: ELEMENT, CDATA, COMMENT. This is used to improve the XML format when using a transformer * to do the formatting (whitespace nodes are interfering with indentation and line breaks). * This method was modeled after a method by "user2401669" found on * <a href="http://stackoverflow.com/questions/16641835/strange-xml-indentation">StackOverflow</a>. *//*from ww w . ja va 2 s. c o m*/ public static void cleanEmptyTextNodes(Node parentNode) { boolean removeEmptyTextNodes = false; Node childNode = parentNode.getFirstChild(); while (childNode != null) { short nodeType = childNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) { removeEmptyTextNodes = true; if (nodeType == Node.ELEMENT_NODE) { cleanEmptyTextNodes(childNode); // recurse into subtree } } childNode = childNode.getNextSibling(); } if (removeEmptyTextNodes) { removeEmptyTextNodes(parentNode); } }
From source file:Main.java
public static void spreadNamespaces(Node node, String tns, boolean overwrite) { Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument(); boolean isParent = false; while (node != null) { Node next = null;//from w ww. jav a2 s. c o m if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNamespaceURI() == null) { node = doc.renameNode(node, tns, node.getNodeName()); } else { if (overwrite) { tns = node.getNamespaceURI(); } } NamedNodeMap nodeMap = node.getAttributes(); int nodeMapLengthl = nodeMap.getLength(); for (int i = 0; i < nodeMapLengthl; i++) { Node attr = nodeMap.item(i); if (attr.getNamespaceURI() == null) { doc.renameNode(attr, tns, attr.getNodeName()); } } } isParent = (isParent || (next = node.getFirstChild()) == null) && (next = node.getNextSibling()) == null; node = isParent ? node.getParentNode() : next; if (isParent && node != null) { if (overwrite) { tns = node.getNamespaceURI(); } } } }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * Returns the next sibling element of the specified node. * <p/>/* w ww.ja v a2 s. c o m*/ * If there is no such element, this method returns <code>null</code>. * * @param node the node to process. * @return the next sibling element of the specified node. */ public static Element getNextSiblingElement(Node node) { Node sibling = node.getNextSibling(); if ((sibling == null) || (sibling.getNodeType() == Node.ELEMENT_NODE)) { return (Element) sibling; } return getNextSiblingElement(sibling); }