Java JDOM Element removeAttribute() 方法



Java JDOM 的removeAttribute()方法是Element类的成员方法,用于移除XML文档中的属性。此方法在移除属性后返回一个布尔值。我们可以通过传递Attribute对象或属性的本地名称来移除属性。使用此方法,可以移除特定命名空间内的属性。

语法

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

Element.removeAttribute(attname);
Element.removeAttribute(attname, ns);
Element.removeAttribute(attribute);

参数

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

  • attname − 要移除的属性的名称。
  • ns − 需要移除的属性的命名空间。
  • attribute − XML 元素的 Attribute 对象。

返回值

如果属性被移除,Java removeAttribute() 方法返回 true;否则返回 false。

示例 1

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

<?xml version="1.0" encoding="UTF-8"?>
   <department xmlns:buz ="https://attribute/ns/buz"  buz:id="101" code="CS" id="90">
      <name>Computer Science</name>
      <staffCount>20</staffCount>
   </department>

以下基本的 Java 程序使用 Java JDOM Element removeAttribute() 方法来移除属性。

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

public class RemoveAttribute {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("college.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //remove attribute
	     root.removeAttribute("id");
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

移除'id'属性后的XML文档将被显示。

<?xml version="1.0" encoding="UTF-8"?>
<department xmlns:buz="https://attribute/ns/buz" buz:id="101" code="CS">
  <name>Computer Science</name>
  <staffCount>20</staffCount>
</department>

示例 2

removeAttribute() 方法可用于移除特定命名空间内的属性。

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 RemoveAttribute {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("college.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //remove attribute
	     Namespace ns = Namespace.getNamespace("https://attribute/ns/buz");
	     root.removeAttribute("id",ns);
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

移除前缀为'buz'的'id'属性后的XML文档将被显示。

<?xml version="1.0" encoding="UTF-8"?>
<department xmlns:buz="https://attribute/ns/buz" code="CS" id="90">
  <name>Computer Science</name>
  <staffCount>20</staffCount>
</department>

示例 3

removeAttribute() 方法可用于通过传递 Attribute 对象作为参数来移除属性。

import java.io.File;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class RemoveAttribute {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("college.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //remove attribute
	     List<Attribute> attrList = root.getAttributes();
	     root.removeAttribute(attrList.get(1));
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);	        
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

移除'code'属性后的XML文档将被显示。

<?xml version="1.0" encoding="UTF-8"?>
<department xmlns:buz="https://attribute/ns/buz" buz:id="101" id="90">
  <name>Computer Science</name>
  <staffCount>20</staffCount>
</department>
广告
© . All rights reserved.