Default namespace for XML?

936 views
Skip to first unread message

Norbert Kiesel

unread,
Jul 25, 2022, 2:07:10 PM7/25/22
to jackson-user
Is it possible to configure a default namespace for the parser? Right now I add a @JacksonXmlProperty(namespace=“myns”) to every field.

Tatu Saloranta

unread,
Jul 25, 2022, 5:23:51 PM7/25/22
to jackso...@googlegroups.com
Underlying `XMLStreamWriter` does offer a way to bind the default
namespace, at least in "repairing mode"; if so, calling:

sw.setDefaultNamespace(defaultNsURI);

should bind the default namespace and result in namespace declaration
also being written.

But if so, you will need to manually construct the generator through
XmlFactory or XmlMapper, passing pre-constructed
XMLStreamWriter.
For XmlMapper that'd be method:

public void writeValue(XMLStreamWriter w, Object value) throws
IOException { }

I think there is may be an issue requesting Jackson functionality to
make this easier as well so support may be
added in future.

-+ Tatu +-

On Mon, Jul 25, 2022 at 11:07 AM Norbert Kiesel <nki...@gmail.com> wrote:
>
> Is it possible to configure a default namespace for the parser? Right now I add a @JacksonXmlProperty(namespace=“myns”) to every field.
>
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/6f4bce52-a30d-4692-bf7c-1b10f468a5c8n%40googlegroups.com.

Norbert Kiesel

unread,
Jul 25, 2022, 8:43:21 PM7/25/22
to jackson-user
What you described seems to be for serializing to XML.  However, my issue is to deserialize/parse existing XML with configured and applied NS for all tags. I did not see a corresponding "setDefaultNamespace" for parsing.

Tatu Saloranta

unread,
Jul 26, 2022, 11:59:36 AM7/26/22
to jackso...@googlegroups.com
On Mon, Jul 25, 2022 at 5:43 PM Norbert Kiesel <nki...@gmail.com> wrote:
>
> What you described seems to be for serializing to XML. However, my issue is to deserialize/parse existing XML with configured and applied NS for all tags. I did not see a corresponding "setDefaultNamespace" for parsing.

Oh! My bad then.

No, there is no such functionality and it would seem to me to be
against XML specification: namespace binding information must be
contained within the incoming XML document/content. The default
namespace is by default unbound (namespace URI of "") but can be bound
to non-empty namespace URI. But it is a fundamental property of the
document and not externally configurable.

I suspect I still misunderstand what you are asking?

-+ Tatu +-
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/edf16e04-35cc-43a1-9016-2f26607180aen%40googlegroups.com.

Norbert Kiesel

unread,
Jul 26, 2022, 2:58:10 PM7/26/22
to jackson-user
tldr: Thanks so much for opening my eyes! Problem solved!

Longer version: my mistake was that I assumed I must add an explicit namespace to every mapped field.  Your "it is a fundamental property" made me think: can't I just drop the explicit namespace annotations? And the answer is: yes.

I had XML like <?xml version="1.0" encoding="UTF-8"?><mdf:flow xmlns:mdf="http://www.foo.com/mdf"><mdf:code>code123</mdf:code><mdf:name>myName</mdf:name></mdf:flow> and I thought I had to explicitly add @JacksonXmlProperty(namespace = "http://www.foo.com/mdf") to every pojo field. But as it turns out, parsing this XML works w/o any annotations!

Tatu Saloranta

unread,
Jul 26, 2022, 5:12:56 PM7/26/22
to jackso...@googlegroups.com
On Tue, Jul 26, 2022 at 11:58 AM Norbert Kiesel <nki...@gmail.com> wrote:
>
> tldr: Thanks so much for opening my eyes! Problem solved!
>
> Longer version: my mistake was that I assumed I must add an explicit namespace to every mapped field. Your "it is a fundamental property" made me think: can't I just drop the explicit namespace annotations? And the answer is: yes.
>
> I had XML like <?xml version="1.0" encoding="UTF-8"?><mdf:flow xmlns:mdf="http://www.foo.com/mdf"><mdf:code>code123</mdf:code><mdf:name>myName</mdf:name></mdf:flow> and I thought I had to explicitly add @JacksonXmlProperty(namespace = "http://www.foo.com/mdf") to every pojo field. But as it turns out, parsing this XML works w/o any annotations!

Ah. Ok, yes, I was overcomplicating the problem in my head. Now your
question makes perfect sense.
Thank you for explaining things; I jumped to wrong conclusions early
(based on other challenges with namespaces).

What you found out is true, of course, but one caveat: if you need to
write XML, you do need those namespace declarations for
interoperability.
The fact that Jackson XML module accepts content using just the local
name can be seen both as a feature and a bug. :)
(meaning: some users would prefer strict matching to catch potentially
incorrect XML)

-+ Tatu +-

ps. There probably is another feature request in here: it would be
"Default XML namespace for Java types" instead of "Default XML
namespace within XML document". I'll keep that in mind for the future.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/d556c376-5d56-43d4-9d09-37c1d6580038n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages