question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing `Map`s will produce non-well-formed XML as keys are not modified as per XML name rules

See original GitHub issue

Hello, I can not parse Map to valid XML String if Map contains a key with white space. I am using jackson-dataformat-xml version: 2.5.3

Here is sample code to reproduce this issue:


package com.asite.html;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Test {
	public static void main(String[] args) throws IOException{
		parseMapWithNoSpace(); //This method works fine
		parseMapWithSpace(); //This method will throw IOException
	}
	private static void parseMapWithNoSpace() throws JsonProcessingException, IOException, JsonParseException, JsonMappingException {
		Map<String, String> mapWithNoSpace = new HashMap<>();
		mapWithNoSpace.put("mapWithNoSpace", "test test"); //Key with no space
		XmlMapper mapper = new XmlMapper();
		String xml = mapper.writeValueAsString(mapWithNoSpace);
		System.out.println(xml);
		Map map1 = mapper.readValue(xml, Map.class);
		System.out.println(map1);
	}

	private static void parseMapWithSpace() throws JsonProcessingException, IOException, JsonParseException, JsonMappingException {
		Map<String, String> mapWithSpace = new HashMap<>();
		mapWithSpace.put("tests pace", "test"); //key with space
		XmlMapper mapper = new XmlMapper();
		String xml = mapper.writeValueAsString(mapWithSpace);
		System.out.println(xml);
		Map map1 = mapper.readValue(xml, Map.class);
		System.out.println(map1);
	}
}

Here is console output


<HashMap xmlns=""><mapWithNoSpace>test test</mapWithNoSpace></HashMap>
{mapWithNoSpace=test test}
<HashMap xmlns=""><test space>test</test space></HashMap>
Exception in thread "main" java.io.IOException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,30]
Message: Attribute name "space" associated with an element type "test" must be followed by the ' = ' character.
	at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwXmlAsIOException(StaxUtil.java:24)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.next(XmlTokenStream.java:164)
	at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.nextToken(FromXmlParser.java:453)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:461)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:342)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2578)
	at com.asite.html.Test.parseMapWithSpace(Test.java:33)
	at com.asite.html.Test.main(Test.java:15)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,30]
Message: Attribute name "space" associated with an element type "test" must be followed by the ' = ' character.
	at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601)
	at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
	at org.codehaus.stax2.ri.Stax2ReaderAdapter.next(Stax2ReaderAdapter.java:129)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream._collectUntilTag(XmlTokenStream.java:349)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream._next(XmlTokenStream.java:312)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.next(XmlTokenStream.java:162)
	... 8 more



Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
cfriescommented, Apr 25, 2019

This issue is more severe than it looks. Jackson cannot serialise the following simple Java Data Structures into valid XML:

  • Map<LocalDate, Object>
  • Map<Integer, Object>
  • Map<Double, Object>

etc. The issue being that XML Tags are not allowed to start with a number.

1reaction
nitinvavdiyacommented, Jun 20, 2017

@cowtowncoder

I am not generating this XML, XmlMapper does. This is my question, why XmlMapper is generating invalid XML. I can put any String in Map as key.

I am getting this issue while RestTemplate . ie. Client:

ResponseEntity<Map> response = RESTTEMPLATE.postForEntity(INITIALURL + "/getMap", templateParamRequest, Map.class);

Service:

Map<String, String> map = new HashMap<>();
map.put("test space", "test")
return ResponseEntity<>(map, HttpStatus.OK);

I don’t understabd why did you close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DOM Parsing and Serialization - W3C
To produce an XML serialization of a Node node given a flag require well-formed , run the following steps: Let context namespace be...
Read more >
Controlling XML Serialization Using Attributes - Microsoft Learn
public class Book { public string ISBN; } // When an instance of the Book class is serialized, it might // produce this...
Read more >
Chapter 17 Binding between XML Schema and Java Classes
(JAXB 1.0 specified the mapping of XML Schema-to-Java, but not Java-to-XML Schema.) ... There is also no requirement that the Java content tree...
Read more >
encoding/xml - Go Packages
Package xml implements a simple XML 1.0 parser that understands XML name spaces. ... has no explicit name tag as per the previous...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
Default behavior is to infer the column names: if no names are passed the behavior is ... For example, a valid list-like usecols...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found