问题描述
我有一个非常复杂的XML树,它在模式中有几个命名空间。我设法生成相应的Jaxb(使用eclipse IDE)和编组器/解组器工作正常。现在,我希望XML成为特定的格式,因为我需要将它提供给一些系统,我没有别的选择。
生成的XML是:
<?xml version =1.0encoding =UTF-8standalone =no?>
< ns2:data xmlns:ns2 =ns:example-mainxmlns:ns10 =ns:example-main / mynamespace10xmlns:ns11 =http://www.w3.org/1999/ xmlns:ns4 =ns:example-main / mynamespace5xmlns:ns5 =ns:example-main / mynamespace6xmlns:ns6 =ns:example-main / mynamespace111xmlns:xsi =http: //www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =ns:example-main resource:example-main>
< ns6:ruleList>
< ns6:rule>
< ns6:name> DFAC< / ns6:name>
< ns6:className> com.example.Rule< / ns6:className>
< ns6:label> DFAC类< / ns6:label>
< / ns6:rule>
< / ns6:ruleList>
< / ns2:data>
XML我需要生成
<?xml version =1.0encoding =UTF-8standalone =no?>
< data xmlns =ns:example-mainxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =ns:example-main resource:example - 主>
< ruleList xmlns =ns:example-main / Rulexsi:schemaLocation =ns:example-main / Rule resource:Rule>
< rule>
< name> DFAC< / name>
< className> com.example.Rule< / className>
< label> DFAC类< / label>
< / rule>
< / ruleList>
< / data>
我正在使用Jaxb RI 2.2.6
到目前为止我已经做了:
1。
myjaxbMarshaller.setProperty(com.sun.xml.bind.namespacePrefixMapper,新的MyNamespaceMapper()) ;
并从方法返回
-
@XmlSchema使用xmlns注释
@XmlSchema(namespace =example-main,xmlns = {@ javax.xml.bind .notation.XmlNs(namespaceURI =ns:example-main,prefix =))
-
写入DOM树并使用dom的命名空间配置,然后marsahlling dom:
doc.getDomConfig()。setParameter(namespaces,true);
doc.getDomConfig()。setParameter(namespace-declaration,true);
doc.normalizeDocument();
或在上面的行中将命名空间设置为false,以完全摆脱的命名空间,但它无助于
您可以尝试使用 NamespacePrefixMapper
。您必须扩展它并将其设置为属性。
这是一个最简单的例子:
marshaller.setProperty(com.sun.xml.bind.namespacePrefixMapper,
new NamespacePrefixMapper(){
@Override
public String [] getPreDeclaredNamespaceUris() {
return new String [] {MyNamespaces.EXAMPLE_MAIN};
}
@Override
public String getPreferredPrefix(String namespaceUri,String suggestion,boolean requirePrefix){
if(namespaceUri.equals(MyNamespaces.EXAMPLE_MAIN)||
namespaceUri.equals(MyNamespaces.EXAMPLE_MAIN_RULE))
返回;
}
});
I have a very complex XML tree, which has several namespaces in schema. I managed to generate corresponding Jaxb (using eclipse IDE) and marshaller/unmarshaller works fine.
Now, I want the XML into specific format, because i need to feed it to some system, and I have no other option.
The generated XML is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns2:data xmlns:ns2="ns:example-main" xmlns:ns10="ns:example-main/mynamespace10" xmlns:ns11="http://www.w3.org/1999/XSL/Transform" xmlns:ns4="ns:example-main/mynamespace5" xmlns:ns5="ns:example-main/mynamespace6" xmlns:ns6="ns:example-main/mynamespace111" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ns:example-main resource:example-main">
<ns6:ruleList>
<ns6:rule>
<ns6:name>DFAC</ns6:name>
<ns6:className>com.example.Rule</ns6:className>
<ns6:label>DFAC class</ns6:label>
</ns6:rule>
</ns6:ruleList>
</ns2:data>
XML I need to generate
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data xmlns="ns:example-main" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ns:example-main resource:example-main">
<ruleList xmlns="ns:example-main/Rule" xsi:schemaLocation="ns:example-main/Rule resource:Rule">
<rule>
<name>DFAC</name>
<className>com.example.Rule</className>
<label>DFAC class</label>
</rule>
</ruleList>
</data>
I'm using Jaxb RI 2.2.6What I have done so far:1.
myjaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespaceMapper());
and return "" from method
@XmlSchema annotation with xmlns
@XmlSchema(namespace="example-main",xmlns={@javax.xml.bind.annotation.XmlNs(namespaceURI="ns:example-main", prefix=""))
writing into DOM tree and using dom's namespace configurations and then marsahlling dom:
doc.getDomConfig().setParameter("namespaces", true);doc.getDomConfig().setParameter("namespace-declarations", true);doc.normalizeDocument();
or seting namespaces to false in above lines, to completely get rid of namespace, but it doesn't help either
You can try using NamespacePrefixMapper
. You have to extend it and set it as a property.
Here's an example of the shortest method:
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
new NamespacePrefixMapper() {
@Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { MyNamespaces.EXAMPLE_MAIN };
}
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if (namespaceUri.equals(MyNamespaces.EXAMPLE_MAIN) ||
namespaceUri.equals(MyNamespaces.EXAMPLE_MAIN_RULE))
return "";
}
});
这篇关于XML编组:我想要命名空间属性,而不是前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!