On Tue, Jul 26, 2022 at 11:58 AM Norbert Kiesel <[email protected]> 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. > > On Tuesday, July 26, 2022 at 8:59:36 AM UTC-7 Tatu Saloranta wrote: >> >> On Mon, Jul 25, 2022 at 5:43 PM Norbert Kiesel <[email protected]> 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 +- >> >> >> > >> > On Monday, July 25, 2022 at 2:23:51 PM UTC-7 Tatu Saloranta wrote: >> >> >> >> 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 <[email protected]> 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 [email protected]. >> >> > To view this discussion on the web visit >> >> > https://groups.google.com/d/msgid/jackson-user/6f4bce52-a30d-4692-bf7c-1b10f468a5c8n%40googlegroups.com. >> > >> > -- >> > 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 [email protected]. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/jackson-user/edf16e04-35cc-43a1-9016-2f26607180aen%40googlegroups.com. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jackson-user/d556c376-5d56-43d4-9d09-37c1d6580038n%40googlegroups.com. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10gFc49bh1QHvzDuYFMU%3DPRhAVHE6D7OoOjy%3D5%2BvxXMnDQ%40mail.gmail.com.
