简体   繁体   English

XML编组:我想要名称空间属性而不是前缀

[英]XML Marshalling: I want namespace attributes and not prefixes

I have a very complex XML tree, which has several namespaces in schema. 我有一个非常复杂的XML树,该树在架构中具有多个名称空间。 I managed to generate corresponding Jaxb (using eclipse IDE) and marshaller/unmarshaller works fine. 我设法生成了相应的Jaxb(使用Eclipse IDE),并且marshaller / unmarshaller运行正常。

Now, I want the XML into specific format, because i need to feed it to some system, and I have no other option. 现在,我希望将XML转换为特定格式,因为我需要将其提供给某些系统,并且没有其他选择。

The generated XML is: 生成的XML为:

    <?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

<?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.6 What I have done so far: 1. 我正在使用Jaxb RI 2.2.6,到目前为止我已经做了:1。

myjaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespaceMapper());

and return "" from method 并从方法返回“”

  1. @XmlSchema annotation with xmlns 带有xmlns的@XmlSchema批注

    @XmlSchema(namespace="example-main",xmlns={@javax.xml.bind.annotation.XmlNs(namespaceURI="ns:example-main", prefix="")) @XmlSchema(namespace =“ example-main”,xmlns = {@ javax.xml.bind.annotation.XmlNs(namespaceURI =“ ns:example-main”,prefix =“”)))

  2. writing into DOM tree and using dom's namespace configurations and then marsahlling dom: 写入DOM树并使用dom的命名空间配置,然后封送dom:

    doc.getDomConfig().setParameter("namespaces", true); doc.getDomConfig()。setParameter(“ namespaces”,true); doc.getDomConfig().setParameter("namespace-declarations", true); doc.getDomConfig()。setParameter(“ namespace-declarations”,true); doc.normalizeDocument(); doc.normalizeDocument();

or seting namespaces to false in above lines, to completely get rid of namespace, but it doesn't help either 或在上述各行中将名称空间设置为false,以完全摆脱名称空间,但这无济于事

You can try using NamespacePrefixMapper . 您可以尝试使用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 "";

            }
        }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM