views:

590

answers:

3

Hi,

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 samples 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"&gt;

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";
A: 

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?

skaffman
Yes, I send without it and returns error<Result> <MessageID>0</MessageID> <ResultCode>Error</ResultCode> <ResultMessageCode>6001</ResultMessageCode> <ResultDescription>XML parse error at line 2, column 17: Unknown element 'AmazonEnvelope'</ResultDescription> <AdditionalInfo> <SKU>0</SKU> </AdditionalInfo> </Result>
Noura
+1  A: 

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.

Noura
A: 

Thanks, Noura, the marshaller property setting did the trick for me. I actually didn't need to do anything with the NamespacePrefixMapper either. Just setting the said property worked just fine. Thanks!

Raj