List of usage examples for org.w3c.dom Element removeChild
public Node removeChild(Node oldChild) throws DOMException;
oldChild
from the list of children, and returns it. From source file:Main.java
/** @param text <code>null</code> removes 'subElement' completely */ public static Text setTextField(Element node, String subElement, String text) { NodeList nl = node.getElementsByTagName(subElement); if (nl == null || nl.getLength() == 0) { if (text == null) return null; Document d = node.getOwnerDocument(); Element e = d.createElement(subElement); Text ans = d.createTextNode(text); e.appendChild(ans);//from w w w.j a v a 2 s . co m node.appendChild(e); return ans; } else if (text != null) { return setText(nl.item(0), text); } else { node.removeChild(nl.item(0)); return null; } }
From source file:Main.java
public static Document removeFromRootElement(Document document, String nodeName, String attributeName, String attributeValue) {/*from w ww.j av a2 s.com*/ try { Element rootElement = document.getDocumentElement(); NodeList nl = document.getElementsByTagName(nodeName); Vector<Node> deletedNodes = new Vector<Node>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); String noteAttributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue(); if (noteAttributeValue.equals(attributeValue)) { deletedNodes.add(node); } } for (Node deletedNode : deletedNodes) { rootElement.removeChild(deletedNode); } } catch (Exception e) { e.printStackTrace(); } return document; }
From source file:Main.java
public static void cleanWhiteSpaceNodes(Element node, boolean deep) { NodeList list = node.getChildNodes(); ArrayList temp = new ArrayList(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i);/* w w w . ja v a2 s. co m*/ short type = n.getNodeType(); if (type == 1) { Element e = (Element) n; cleanWhiteSpaceNodes(e, deep); } else if (type == 3) { Text text = (Text) n; String val = text.getData(); if (val.trim().equals("")) temp.add(text); } } for (Iterator i = temp.iterator(); i.hasNext(); node.removeChild((Node) i.next())) ; }
From source file:Main.java
private static void removeEmptyChildElements(Element parentElement) { List<Element> toRemove = new LinkedList<Element>(); NodeList children = parentElement.getChildNodes(); int childrenCount = children.getLength(); for (int i = 0; i < childrenCount; ++i) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; removeEmptyChildElements(childElement); if (elementIsRedundant(childElement)) { toRemove.add(childElement); }// w ww .ja v a2 s . c o m } } for (Element childElement : toRemove) { parentElement.removeChild(childElement); } parentElement.normalize(); }
From source file:XMLUtils.java
/** * Sets the text value for a given element. * @param el//from w w w .j av a 2 s . c o m * @param value */ public static void setText(Element el, String value) { // remove the children if already exist while (el.getFirstChild() != null) { el.removeChild(el.getFirstChild()); } if (value == null) { value = ""; } Text txt = el.getOwnerDocument().createTextNode(value); el.appendChild(txt); }
From source file:XMLUtils.java
/** * Sets the text value for a given element as a CDATA section * @param el//from www . ja v a 2 s.c om * @param value */ public static void setCDATA(Element el, String value) { // remove the children if already exist while (el.getFirstChild() != null) { el.removeChild(el.getFirstChild()); } if (value == null) { value = ""; } CDATASection txt = el.getOwnerDocument().createCDATASection(value); el.appendChild(txt); }
From source file:Main.java
public static void setElementTextValue(Element e, String text, boolean cdata) { Document root = e.getOwnerDocument(); e.normalize();//from w w w . j a va 2 s. c o m if (e.hasChildNodes()) { NodeList nl = e.getChildNodes(); /* This suxx: NodeList Object is changed when removing children !!! I will store all nodes that should be deleted in a Vector and delete them afterwards */ int length = nl.getLength(); List<Node> v = new ArrayList<Node>(nl.getLength()); for (int i = 0; i < length; i++) if (nl.item(i) instanceof CharacterData) v.add(nl.item(i)); for (Node n : v) e.removeChild(n); } if (cdata) { e.appendChild(root.createCDATASection(text)); } else { e.appendChild(root.createTextNode(text)); } }
From source file:com.evolveum.midpoint.prism.util.PrismUtil.java
public static void unfortifyNamespaceDeclarations(Element definitionElement) { Map<String, String> namespaces = new HashMap<String, String>(); for (Element childElement : DOMUtil.listChildElements(definitionElement)) { if (PrismConstants.A_NAMESPACE.equals(DOMUtil.getQName(childElement))) { String prefix = childElement.getAttribute(PrismConstants.A_NAMESPACE_PREFIX); String namespace = childElement.getAttribute(PrismConstants.A_NAMESPACE_URL); namespaces.put(prefix, namespace); definitionElement.removeChild(childElement); } else {/*from w w w. j a va2s . c o m*/ unfortifyNamespaceDeclarations(definitionElement, childElement, namespaces); namespaces = new HashMap<String, String>(); } } }
From source file:mondrian.test.DiffRepository.java
private static void removeAllChildren(Element element) { final NodeList childNodes = element.getChildNodes(); while (childNodes.getLength() > 0) { element.removeChild(childNodes.item(0)); }//from w w w. j a v a2 s . co m }
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Removes the child element which has a the specified attribute and value. * // ww w .j a v a2s . co m * @param node * the root of the tree to search * @param tagName * @return whether something was removed */ public static boolean removeEltInDescentByAttrValue(Element node, String attr, String value) { boolean res; if (node != null) { List<Element> children = getAllChildren(node); for (Element elt : children) { String actualValue = elt.getAttribute(attr); if (StringUtils.equals(actualValue, value)) { node.removeChild(elt); return true; } } // not found yet, try in each child for (Element elt : children) { res = removeEltInDescentByAttrValue(elt, attr, value); if (res == true) { return true; } } } return false; }