开发者

Add Xml namespace using Jaxb

开发者 https://www.devze.com 2022-12-21 05:28 出处:网络
I am creating amazon feed, the feed xml should be like: <AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"

I am creating amazon feed, the feed xml should be like:

<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">

I am using jaxb to generate xml files from java classes, I used NamespacePrefixMapperImpl from jaxb sa开发者_运维知识库mples to add namespaces to the xml. But currently it generates the root like:

<AmazonEnvelope xmlns:xsi:noNamespaceSchemaLocation="amzn-envelope.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

how I can remove the xmlns from amzn-envelope.xsd??

Here's the mapping I used in NamespacePrefixMapperImpl

if( "http://www.w3.org/2001/XMLSchema-instance".equals(namespaceUri) )
    return "xsi";

if("amzn-envelope.xsd".equals(namespaceUri))
    return "xsi:noNamespaceSchemaLocation";


I found a property at Marshaller that can add the amzn-envelope.xsd:

marshaller.setProperty("jaxb.noNamespaceSchemaLocation", "amzn-envelope.xsd");

and left the NamespacePrefixMapper to add the "http://www.w3.org/2001/XMLSchema-instance". Hope this helps others.


If I understand your intent, your document has a default namespace, and you're trying to add the schemalLocation for that namespace.

NamespacePrefixMapper won't let you do this, it's useful only for picking a prefix for a namespace. There are no namespaces in this document, and so no useful way of using NamespacePrefixMapper. It can't be used for adding schemaLocation hints - those are treated specially by JAXB, and you're just confusing it.

Are you sure you need the noNamespaceSchemaLocation="amzn-envelope.xsd" at all? Have you tried sending it to the web service without it?

0

精彩评论

暂无评论...
验证码 换一张
取 消