Hi Chris,
I'll try to answer the remaining parts of your questions (after Eitan's response).
I think the main issue here is that you're not specifying a name for the mapping, but instead letting the marshaller/unmarshaller control the name. When you do this there's no way for JiBX to set the namespace automatically. I think if you make your marshaller/unmarshaller class aliasable (which just means that it can be subclassed to work with a name and namespace specified by the binding), and add a name="error" attribute on your <mapping> element it'll behave the way you want.
Adding namespaces which aren't included in the binding definition is somewhat ugly, and require you to use the IXMLWriter instance directly. You have to first define the extension namespaces, then write them out to an element start tag. Here's a sample of how this is done in the JibxSoap implementation:
m_marshaller.setOutput(os, null);
m_marshaller.startDocument("UTF-8", Boolean.TRUE);
IXMLWriter xwrite = m_marshaller.getXmlWriter();
int nsi = xwrite.getNamespaces().length;
xwrite.pushExtensionNamespaces(SOAP_URI_ARRAY);
xwrite.startTagNamespaces(nsi, SOAP_ENVNAME,
new int[] {nsi}, SOAP_PREFIX_ARRAY);
xwrite.closeStartTag();I'd like to work out a cleaner way of handling this in 2.0, but like most things to do with namespaces in XML it's a lot more complicated than it might appear to handle this properly.
- Dennis
Chris Chen wrote:
... I have a few questions that hopefully you all can help me out with. ... 3) I have a simply binding file:
<binding> <mapping class="ErrorPacket" marshaller="ErrorPacketMapper" unmarshaller="ErrorPacketMapper"/> </binding>
Suppose that the error packet is as follows:
<stream:error xmlns:stream="stream:error"> <blah/> </stream:error>
When running tests to unmarshall this XML, I get the error:
No unmarshaller for element "{stream:error}error" (line 1, col 62)
Ok, so then I change the binding file to this:
<binding> <mapping class="ErrorPacket" marshaller="ErrorPacketMapper" unmarshaller="ErrorPacketMapper" ns="stream:error"/> </binding>
So now, it will work properly and unmarshalling goes as expected.
Now if I do this:
<binding> <namespace prefix="stream" uri="stream:error" default="elements"/> <mapping class="ErrorPacket" marshaller="ErrorPacketMapper" unmarshaller="ErrorPacketMapper"/> </binding>
It doesn't work the way I wanted, and I get the same error as mentioned above. The following binding file works:
<binding> <namespace prefix="stream" uri="stream:error" default="elements"/> <mapping class="ErrorPacket" marshaller="ErrorPacketMapper" unmarshaller="ErrorPacketMapper" ns="stream:error"/> </binding>
I am curious why I have to specify the ns attribute when the namespace should be global and apply to the mapping. Or am I wrong that the <namespace> configuration actually only applies to child elements of the main element?
4) Also, with the above working binding file, I am trying to marshall the object to XML. Supposedly, with the <namespace> specifying stream as prefix, I assume that during marshalling, the MarshallingContext should already contain a mapping that the error element should have the "stream" prefix prepended. However, it doesn't. Debugging and printing out the Namespace Prefix for every index shows null for the URI "stream:error". Why is that? Is there a solution?
5) How do I add add my own Namespace URI in the marshall() method that is not defined in the binding file? Do i add them as extension namespaces?
6) In order to print out the namespace properly, do I have to use the MarshallingContext.startTagNamespaces()?
I hope you can answer some, or all, of the questions I have. It is rather difficult when getting down to the marshalling and unmarshalling code. It's a bit more complicated than just binding files.
Thanks, Chris
------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ jibx-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/jibx-users
