Remove ns2 as default namespace prefix(移除 ns2 作为默认命名空间前缀)
问题描述
我有一个使用默认命名空间打印的文件.元素以 ns2 前缀打印,我需要将其删除,我的代码如何:
I have a file that is printed with a default namespace. The elements are printed with a prefix of ns2, I need this to be removed, how it is with my code:
<ns2:foo xmlns:ns2="http://namespace" />
我希望它是怎样的:
<foo xmlns="http://namespace" />
这就是我的编码方式,我认为这应该足以让 ns2 消失:
this is how I have coded it, something which as I see it should be enough for the ns2 to go away:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:bar="http://namespace" targetNamespace="http://namespace"
elementFormDefault="qualified">
...
生成的包信息是这样的:
the generated package-info turns out like this:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.foo.bar;
我这样创建文件:
JAXBContext jaxbContext = JAXBContext.newInstance(generatedClassesPackage);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<Foo>(new QName("http://namespace", "Foo"),
Foo.class, rootFoo), outputStream);
generatedClassesPackage 是 package-info.java 和元素所在的包.
generatedClassesPackage is the package where package-info.java and the elements are.
Foo 对象已定义并具有如下元素::
The Foo object is defined and has elements like this::
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"group"
})
@XmlRootElement(name = "Foo")
public class Foo {
@XmlElement(name = "Group", required = true)
protected List<Group> group;
我错过了什么吗?还是我误解了它的工作原理?
Is it something I have missed? or have I misunderstood how this works?
推荐答案
您很可能在响应中有多个命名空间.这将使用创建 ns# 命名空间前缀的默认约定,其中一个成为没有前缀的 xmlns.如果您想控制它,您可以执行以下操作:
Most likely you have multiple namespaces in the response. This will use the default convention of creating ns# namespace prefixes and one of them becomes the xmlns without a prefix. If you want to control this you can do the following:
NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if ("http://namespace".equals(namespaceUri) && !requirePrefix)
return "";
return "ns";
}
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
marshaller.mashal....
这将始终将 http://namespace
设置为默认 xmlns,并在编组时将 ns# 用于所有其他命名空间.如果你愿意,你也可以给他们更多的描述性前缀.
This will set the http://namespace
as the default xmlns always and use ns# for all other namespaces when marshalling. You can also give them more descriptive prefixes if you want.
这篇关于移除 ns2 作为默认命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:移除 ns2 作为默认命名空间前缀
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01