Skip to content

XML to JSON - IOException when parsing XML with XMLMapper #226

@dbories

Description

@dbories

Hello,

I found a weird error when trying to parse XML documents with an com.fasterxml.jackson.dataformat.xml.XmlMapper. If an XML element has no attribute and contains both text nodes and sub-elements, the XML parser fails with the following exception:

 Exception in thread "main" java.io.IOException: Expected END_ELEMENT, got event of type 1
 	at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.skipEndElement(XmlTokenStream.java:180)
 	at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.nextToken(FromXmlParser.java:558)
 	at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:223)
 	at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:69)
 	at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15)
 	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
 	at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2381)
 	at test.mvn.TestJackson.main(TestJackson.java:18)

Example of XML document that produces the error

<?xml version="1.0" encoding="UTF-8"?>
 <root>
   <a>lorem <b>ipsum</b> dolor</a>
 </root>

If the parent element has at least one attribute, the parsing is OK.
Example of XML document that does not produce any error:

<?xml version="1.0" encoding="UTF-8"?>
 <root>
   <a id="1">lorem <b>ipsum</b> dolor</a>
 </root>

The java code used:

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class TestJackson {

  public static void main(
      final String[] args) throws IOException {
    final String iString = FileUtils.readFileToString(new File("C:/test.xml"), StandardCharsets.UTF_8);

    new XmlMapper().readTree(iString);
  }

}

Reproduced with 2.8.0 and 2.8.7
Not reproduced with 2.7.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions