Java JDOM Element addNamespaceDeclaration() 方法



Java JDOM 的addNamespaceDeclaration() 方法 (Element 類的方法) 用於為 XML 元素新增名稱空間。此方法在成功將名稱空間新增到元素後返回 true。如果名稱空間已存在,則返回 false。當新增具有已存在字首的名稱空間時,它會丟擲 IllegalAddException。

語法

以下是 Java JDOM Element addNamespaceDeclaration() 方法的語法:

Element.addNamespaceDeclaration(namespace);

引數

Java addNamespaceDeclaration() 方法接受單個引數。

namespace - 表示需要新增的 Namespace 物件。

返回值

Java addNamespaceDeclaration() 方法返回一個布林值;如果名稱空間已存在,則返回 false,否則返回 true。

示例 1

以下基本示例使用 Java JDOM Element addNamespaceDeclaration() 方法將名稱空間新增到根元素。

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

public class AddNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/root");
	     //Add nameSpace
	     root.addNamespaceDeclaration(ns);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

新增名稱空間到根元素後的文件將顯示。

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:prefix="https://namespaces/root">I'm root.</root>

示例 2

如果我們嘗試新增的名稱空間已經宣告,則 addNamespaceDeclaration() 方法將返回 false。

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

public class AddNamespace {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/root");
	     //Add nameSpace
	     Boolean status = root.addNamespaceDeclaration(ns);  
	     System.out.println(status);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

輸出視窗返回 false。

false

示例 3

當嘗試新增具有相同字首的名稱空間時,addNamespaceDeclaration() 方法會丟擲 IllegalAddException。

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

public class AddNamespace {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
    	 SAXBuilder saxBuilder = new SAXBuilder();
    	 File inputFile = new File("sample.xml");
    	 Document doc = saxBuilder.build(inputFile);
    	 Element root = doc.getRootElement();
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/info");
	     //Add nameSpace
	     root.addNamespaceDeclaration(ns);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

輸出視窗顯示錯誤。

org.jdom2.IllegalAddException: The namespace xmlns:prefix="https://namespaces/info" could not be added as a namespace to "root": The namespace prefix "prefix" collides with an additional namespace declared by the element
	at org.jdom2.Element.addNamespaceDeclaration(Element.java:400)
	at addNSdeclaration.Example3.main(Example3.java:20)
廣告