How do I add a namespace attribute to an element in JAXB when marshalling?
As far as I can tell, your XML fragments are semantically identical. The xmlns
attribute on the AddFixedPriceItemRequest
element is redundant, since it implicitly inherits the namespace of its parent element. JAXB knows this, and so doesn't bother adding the namespace to AddFixedPriceItemRequest
- it's just not necessary.
If the ebay server is only working when the AddFixedPriceItemRequest
xmlns
is present, then it's broken, and is making demands on the input over and above those required by XML and by the schema. If this is indeed the case (which is hard to believe, but possible), then using a Java XML document model like JAXB is going to be a struggle, since that will assume XML is XML is XML. Low-level farting about with which elements get the xmlns
declarations is not exposed to the API, since it shouldn't be needed.
None of which is helping you. My approach would be to marshal the JAXB model to a DOM object (using a DOMResult
passed to the Marshaller
), and then see if you can do some manual tweaking of the DOM to force the xmlns
into the document at the appropriate places. You can then serialize that DOM to XML and send that.
You shouldn't have to do this, and I suspect you may be doing something else wrong somewhere; this is more likely than the ebay web service being broken like this.
edit: here's another suggestion, a bit less awful than the JAXB-to-DOM-to-XML solution. If your request XML is reasonable static in structure, with only the numeric/string values changing, then define it as a String template, then replace the values at runtime, and send that. You can then interpret the results using JAXB. I've done this in the oast with web services thyat required very exact namespace prefixes, when persuading the java XML libraries to conform to that was unreasonably hard.
Try to use class annotation
@XmlType(namespace="urn:ebay:apis:eBLBaseComponents")
or
@XmlElement(namespace="urn:ebay:apis:eBLBaseComponents")
property annotation if you only want to specify namespace only in some certain cases
Check if the fields in a generated classed are missing the @XmlElement
annotation and if present are they missing the namespace attribute. These two must be present in order to get the namespace prefix against every element in your marshaled xml.