Java JDOM Element getAttributeValue() 方法



Java JDOM 的 getAttributeValue() 方法是 Element 类的方法,用于获取指定属性的属性值。使用此方法,我们可以获取特定命名空间内属性的值。

如果不存在作为参数提供的属性,则 getAttributeValue() 方法返回 null。如果属性存在但未附加值,则返回空字符串。

语法

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

Element.getAttributeValue(attname);
Element.getAttributeValue(attname, def);
Element.getAttributeValue(attname, ns);
Element.getAttributeValue(attname, ns, def);

参数

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

  • attname - 需要查找其值的属性名称。
  • def - 如果属性不存在,则应返回的默认值。
  • ns - 附加到需要查找的属性的命名空间。

返回值

Java getAttributeValue() 方法以字符串的形式返回指定属性的值。

示例 1

这是我们需要解析的 department.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 getAttributeValue() 方法的使用:

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

public class GetAttributeValue {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("department.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //Get attribute value
	     String attrValue = root.getAttributeValue("id");
	     System.out.println("Department id : "+attrValue);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示 department 元素的部门 ID。

Department id : 90

示例 2

getAttributeValue() 方法也可以用于在指定属性对于 Element 不存在时返回默认值。

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

public class GetAttributeValue {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("department.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //Get attribute value
	     String attrValue = root.getAttributeValue("blockId","Not found");
	     System.out.println("Department Block id : "+attrValue);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

部门块 ID 显示为未找到。

Department Block id : Not found

示例 3

getAttributeValue() 方法用于获取附加到特定命名空间的属性的属性值。

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

public class GetAttributeValue {
   public static void main(String args[]) {
      try {	
    	 //Read the document and get the root
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("department.xml");
    	 Document doc = saxBuilder.build(inputFile);
	     Element root = doc.getRootElement();
	     //create namespace object
	     Namespace ns = Namespace.getNamespace("buz","https://attribute/ns/buz");
	     //Get attribute value
	     String attrValue = root.getAttributeValue("id",ns);
	     System.out.println("Department id with namespace 'buz': "+attrValue);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

显示与提供的命名空间关联的部门 ID。

Department id with namespace 'buz': 101
广告

© . All rights reserved.