Skip to content

IOException when parsing XML with XMLMapper #361

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

Closed
dbories opened this issue Mar 10, 2017 · 3 comments
Closed

IOException when parsing XML with XMLMapper #361

dbories opened this issue Mar 10, 2017 · 3 comments

Comments

@dbories
Copy link

dbories commented Mar 10, 2017

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

@cowtowncoder
Copy link
Member

Since this is related to XML handling, this bug should be against jackson-dataformat-xml, so could you re-file at:

https://github.com/FasterXML/jackson-dataformat-xml/issues

@dbories
Copy link
Author

dbories commented Mar 13, 2017

Indeed.
I got my tabs mixed up. I'll repost my bug there. Thanks.

@thisismana
Copy link

Here is the corresponding issue FasterXML/jackson-dataformat-xml#226

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants