Java JDOM Element setNamespace() 方法



Java JDOM 的 setNamespace() 方法是 Element 类的方法,用于设置 XML 元素的命名空间。如果 XML 元素没有任何命名空间,则添加提供的命名空间;否则,将替换已存在的命名空间。如果存在命名空间冲突,此方法将抛出 IllegalAddException 异常。

语法

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

Element.setNamespace(ns);

参数

Java Element.setNamespace() 方法接受一个参数:

ns - 表示要添加的命名空间对象。

返回值

Java setNamespace() 方法在添加命名空间后返回 XML 元素。

示例 1

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

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

public class SetNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
    	 Document doc = new Document();
	     Element root = new Element("book");
         //Set namespace	     
	     Namespace ns = Namespace.getNamespace("https://namespace/bookNS");
	     root.setNamespace(ns);
	     doc.setRootElement(root);
	     //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="https://namespace/bookNS" />

示例 2

当将 null 作为参数传递给 setNamespace() 方法时,它将设置无命名空间。

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

public class SetNoNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
    	 Document doc = new Document();
	     Element root = new Element("book");
         //Set namespace	     
	     root.setNamespace(null);
	     doc.setRootElement(root);
	     //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 />

示例 3

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

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="https://namespace/bookNS" />

setNamespace() 方法将用提供的命名空间替换已存在的命名空间。

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 UpdateNamespace {
   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();
		 //Set namespace
    	 Namespace ns = Namespace.getNamespace("https://namespace/newBookNS");
    	 root.setNamespace(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="https://namespace/newBookNS" />
广告

© . All rights reserved.