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)
广告
© . All rights reserved.