Java JDOM Element removeChildren() 方法



Java JDOM 的 removeChildren() 方法是 Element 类的方法,用于从当前元素中移除一层深度的子元素。使用此方法,我们可以移除所有具有相同本地名称并在特定命名空间内的子元素。

语法

以下是 Java JDOM Element removeChildren() 方法的语法:

Element.removeChildren(cname);
Element.removeChildren(cname, ns);

参数

Java removeChildren() 方法是一个多态方法,它接受以下参数:

  • cname - 表示要移除的子元素的本地名称。
  • ns - 表示子元素的命名空间。

返回值

Java removeChildren() 方法以布尔变量的形式返回移除状态。

示例 1

我们需要解析以下 stationary.xml 文件:

<stationary>
   <pre:pencil xmlns:pre = "https://namespace/pencil">Apsara</pre:pencil>
   <pencil>Nataraj</pencil>
   <book>Camlin</book>
   <pen>Cello</pen>
</stationary>

以下是使用 Java JDOM Element removeChildren() 方法的基本示例:

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class RemoveChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("stationary.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Remove child elements
    	 Boolean removed = root.removeChildren("pencil");
    	 System.out.println("removed? " + removed); 
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

输出窗口显示子元素的移除状态。

removed? true

示例 2

如果提供的子元素本地名称与任何子元素的本地名称不匹配,则 removeChildren() 方法返回 false。

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class RemoveChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("stationary.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Remove child elements
    	 Boolean removed = root.removeChildren("eraser");
    	 System.out.println("removed? " + removed); 
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

输出窗口显示子元素的移除状态。

removed? false

示例 3

removeChildren() 方法可用于移除特定命名空间内的子元素。

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class RemoveChildren {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("stationary.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Remove child elements
    	 Namespace ns = Namespace.getNamespace("https://namespace/pencil");
    	 root.removeChildren("pencil", ns);
    	 //Print document
    	 XMLOutputter xmlOutput = new XMLOutputter();
    	 xmlOutput.setFormat(Format.getPrettyFormat());
    	 xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示移除特定子元素后的 XML 文档。

<?xml version="1.0" encoding="UTF-8"?>
<stationary>
  <pencil>Nataraj</pencil>
  <book>Camlin</book>
  <pen>Cello</pen>
</stationary>
广告

© . All rights reserved.