Java JDOM Element setAttribute() 方法



Java JDOM 的 setAttribute() 方法是 Element 类的方法,用于为 XML 元素设置属性。使用此方法,可以设置指定值的属性,也可以设置带命名空间的属性。

如果命名空间与同一元素上的另一个命名空间冲突,则 setAttribute() 方法会抛出 IllegalAddException 异常。如果提供了非法值,则会抛出 IllegalDataException 异常;如果提供的名称对于属性名称是非法的,或者在没有前缀的情况下提供了默认命名空间,则会抛出 IllegalNameException 异常。

语法

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

Element.setAttribute(attribute);
Element.setAttribute(name, value);
Element.setAttribute(name, value, ns);

参数

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

  • attribute - 表示要添加的 Attribute 对象。
  • name - 表示属性的名称。
  • value - 表示属性的值。
  • ns - 表示附加到属性的命名空间。

返回值

Java setAttribute() 方法返回添加了属性的元素。

示例 1

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

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Create attribute
	     Attribute attr = new Attribute("type", "single rule");
	     //Add attribute to root
	     root.setAttribute(attr);
	     //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"?>
<book type="single rule" />

示例 2

setAttribute() 方法以属性的名称和值作为参数,并将它们设置为 XML 元素。

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Add attribute to root
	     root.setAttribute("type", "single rule");
	     //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"?>
<book type="single rule" />

示例 3

setAttribute() 方法设置属性名称、值,如果提供了第三个参数,则还设置命名空间。

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class SetAttribute {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("book");
	     doc.setRootElement(root);
	     //Add attribute to root
	     Namespace ns = Namespace.getNamespace("pre","https://namespace/book");
	     root.setAttribute("type", "single rule",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"?>
<book xmlns:pre="https://namespace/book" pre:type="single rule" />
广告

© . All rights reserved.