Java Utililty Methods XML Element Remove

List of utility methods to do XML Element Remove

  1. HOME
  2. Java
  3. X
  4. XML Element Remove

Description

The list of methods to do XML Element Remove are organized into topic(s).

Method

void remove(Element element, Predicate shouldRemovePredicate)
Removes child elements that meet a certain condition.
NodeList childNodes = element.getChildNodes();
int i = 0;
while (i < childNodes.getLength()) {
 Node currentNode = childNodes.item(i);
 Element child = (currentNode instanceof Element) ? ((Element) currentNode) : null;
 if (child != null && shouldRemovePredicate.test(child)) {
 element.removeChild(currentNode);
 } else {
...
void removeAll(Element node, String subElement)
remove All
for (Element e : findAll(node, subElement))
 node.removeChild(e);
void removeContent(Element element)
remove Content
NodeList nodeList = element.getChildNodes();
while (nodeList.getLength() > 0) {
 element.removeChild(nodeList.item(0));
void removeElements(Element from, String named)
Removes all children named named from element from
NodeList children = from.getChildNodes();
boolean removeNextNewline = false;
List<Node> removes = new ArrayList<Node>();
for (int i = 0; i < children.getLength(); i++) {
 Node child = children.item(i);
 if ((child != null) && named.equals(child.getNodeName())) {
 removes.add(child);
 removeNextNewline = true;
...
Node removeEmptyElements(Node node)
Takes the supplied node and recursively removes empty (no content/child nodes) elements
NodeList list = selectNodeList(node, "*[string-length(normalize-space(.)) = 0]");
for (int i = 0; i < list.getLength(); i++) {
 node.removeChild(list.item(i));
if (node.hasChildNodes()) {
 NodeList childs = node.getChildNodes();
 for (int i = 0; i < childs.getLength(); i++) {
 if (childs.item(i) instanceof Element) {
...
void removePreviousSiblingText(Element element)
Removes any previous siblings text nodes
while (true) {
 Node sibling = element.getPreviousSibling();
 if (sibling instanceof Text) {
 detach(sibling);
 } else {
 break;
void removeSchemaLocation(Element el)
remove Schema Location
NamedNodeMap children;
Node node;
Attr attribute;
String xsi = null;
children = el.getAttributes();
for (int i = 0; i < children.getLength(); i++) {
 node = children.item(i);
 if (node instanceof Attr) {
...
void removeTopPi(Element e, Element pi)
Remove the Pi on top of e
Element piContainer = (Element) pi.getParentNode();
piContainer.replaceChild(e, pi);
void removeWhitespaceNodes(Element e)
Removes whitespace from element content.
NodeList children = e.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
 Node child = children.item(i);
 if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
 e.removeChild(child);
 } else if (child instanceof Element)
 removeWhitespaceNodes((Element) child);
void removeWhitespaceNodes(Element e)
Helper function that removes whitespace nodes from Element objects to allow for easier parsing
NodeList children = e.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
 Node child = children.item(i);
 if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
 e.removeChild(child);
 } else if (child instanceof Element) {
 removeWhitespaceNodes((Element) child);


AltStyle によって変換されたページ (->オリジナル) /