java 在元素上设置命名空间属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11798216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Namespace Attributes on an Element
提问by Paul Reiners
I'm trying to create an XML document in Java that contains the following Element:
我正在尝试用 Java 创建一个包含以下元素的 XML 文档:
<project xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2"
xmlns:acme="http://www.acme.com/schemas"
color="blue">
I know how to create the project Node. I also know how to set the color attribute using
我知道如何创建项目 Node.js。我也知道如何使用设置颜色属性
element.setAttribute("color",
"blue")
element.setAttribute("color",
"blue")
Do I set the xmlns and xmlns:acme attributes the same way using setAttribute() or do I do it in some special way since they are namespace attributes?
我是使用 setAttribute() 以相同的方式设置 xmlns 和 xmlns:acme 属性,还是因为它们是命名空间属性而以某种特殊方式进行设置?
回答by davidfmatheson
I believe that you have to use:
我相信你必须使用:
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:acme", "http://www.acme.com/schemas");
回答by kimbert
The short answer is: you do not create xmlns attributes yourself. The Java XML class library automatically creates those. By default, it will auto-create namespace mappings and will choose prefixes based on some internal algorithm. If you don't like the default prefixes assigned by the Java XML serializer, you can control them by creating your own namespace resolver, as explained in this article:
简短的回答是:您不要自己创建 xmlns 属性。Java XML 类库会自动创建这些。默认情况下,它会自动创建命名空间映射,并会根据一些内部算法选择前缀。如果您不喜欢 Java XML 序列化程序分配的默认前缀,您可以通过创建自己的命名空间解析器来控制它们,如本文所述:
回答by Harsha
I do not think below code will serve the question!
我认为下面的代码不会解决问题!
myDocument.createElementNS("http://www.imsglobal.org/xsd/ims_qtiasiv1p2","project");
This will create an element as below (using DOM)
这将创建一个如下的元素(使用 DOM)
<http://www.imsglobal.org/xsd/ims_qtiasiv1p2:project>
So this will not add an namespace attribute to an element. So using DOM we can do something like
所以这不会向元素添加命名空间属性。所以使用 DOM 我们可以做类似的事情
Element request = doc.createElement("project");
Attr attr = doc.createAttribute("xmlns");
attr.setValue("http://www.imsglobal.org/xsd/ims_qtiasiv1p2");
request.setAttributeNode(attr);
So it will set the first attribute like below, you can set multiple attributes in the same way
所以它会像下面这样设置第一个属性,你可以用同样的方式设置多个属性
<project xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2>
回答by jgerman
The only way that worked for me, in 2019, was using the attr() method:
在 2019 年,对我有用的唯一方法是使用 attr() 方法:
Element element = doc.createElement("project");
element.attr("xmlns","http://www.imsglobal.org/xsd/ims_qtiasiv1p2");
回答by Dave
You can simply specify the namespace when you create the elements. For example:
您可以在创建元素时简单地指定命名空间。例如:
myDocument.createElementNS("http://www.imsglobal.org/xsd/ims_qtiasiv1p2","project");
Then the java DOM libraries will handle your namespace declarations for you.
然后 java DOM 库将为您处理您的命名空间声明。