Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XmlMapper output not well-formed when Object keys use invalid XML name characters #511

Open
rlbns opened this issue Jan 12, 2022 · 1 comment

Comments

@rlbns
Copy link

rlbns commented Jan 12, 2022

I've been working with a lot of web APIs and converting the JSON to XML using XmlMapper. It generally works great, but I have an example that creates invalid XML. I am using the latest version of Jackson (2.13.1).

When you call this API you get the attached JSON output.
https://world.openfoodfacts.org/api/v0/product/7622300315733.json

The Jackson pretty-printer has no problem indenting this nicely.
BadXmlFromJson.zip

After converting it to XML (also in the attached ZIP) you get some issues that prevent Xerces or Saxon from parsing it:

  1. Namespace prefixes are not declared
  2. Some elements are not well formed

For example, at line 48 you see this:
<agribalyse_proxy_food_code:en>12315</agribalyse_proxy_food_code:en>

and at line 653 you see this: <1> which is an error because XML names must start with a letter or underscore.

I'm using XmlMapper like this:

		XmlMapper xmlMapper = new XmlMapper();
		return xmlMapper.writeValueAsString(jsonTree);

I can't find any features in FromXmlParser.Feature or ToXmlGenerator.Feature that apply to either of these issues.

Is there another way to configure XmlMapper or is this a bug?

@cowtowncoder
Copy link
Member

It'd be nice to have test inlined here, instead of as a zip archive. But aside from that yes, there is a problem in using non-XML-name characters in Map keys or as POJO property names -- names will be used as-is, since XML has no mechanism for escaping name characters, so there is no way to safely translate names to contain such characters.

However, #531 (added in 2.14.0) does add a mechanism that might work: it will allow translation using convention which does allow avoiding this problem.

@cowtowncoder cowtowncoder changed the title XmlMapper output not well-formed XmlMapper output not well-formed when Object keys use invalid XML name characters May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants