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.

Strange behavior when using XML attributes

See original GitHub issue

My jackson version 2.8.1:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.8.1</version>
</dependency>

I want to parse XML like this:

<out>
    <in>
        <first>fff</first>
        <second>sss</second>
    </in>
    <in>
        <first>fff2</first>
        <second>sss2</second>
    </in>
</out>

So my Out class:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@ToString
@JacksonXmlRootElement(localName = "out")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Out {

    @JacksonXmlProperty(localName = "in")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<In> ins;

}

In class:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class In {

    @JacksonXmlProperty(localName = "first")
    private String first;
    @JacksonXmlProperty(localName = "second")
    private String second;

}

And App class:

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;

public class App {

    public static void main(String[] args) throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        Out out = xmlMapper.readValue(App.class.getResource("/test.xml"), Out.class);
        System.out.println(out);
    }

}

For XML above everything is good, I get Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)]). But I found few issues:

  1. If I change Out element name to something else everything is working, is it OK?

    <bla-bla>
        <in>
            <first>fff</first>
            <second>sss</second>
        </in>
        <in>
            <first>fff2</first>
            <second>sss2</second>
        </in>
    </bla-bla>
    

    I get Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)])

  2. Lets add some attribute to first element:

    <bla-bla>
        <in>
            <first lang="en">fff</first>
            <second>sss</second>
        </in>
    </bla-bla>
    

    And this fail with Can not construct instance of xmlparsing.In: no String-argument constructor/factory method to deserialize from String value ('sss'). More stranger that if we swap elements then everything will be good:

    <bla-bla>
        <in>
            <second>sss</second>
            <first lang="en">fff</first>
        </in>
    </bla-bla>
    

    I get Out(ins=[In(first=fff, second=sss)])

  3. But this swap can’t help me for a long, because:

    <bla-bla>
        <in>
            <second>sss</second>
            <first lang="en">fff</first>
        </in>
        <in>
            <first>fff2</first>
            <second>sss2</second>
        </in>
    </bla-bla>
    

    I get only second one : Out(ins=[In(first=fff2, second=sss2)])

  4. One more strange thing:

    <bla-bla>
        <in>
            <first lang="en">fff</first>
            <second></second>
        </in>
    </bla-bla>
    

    I get two elements: Out(ins=[In(first=fff, second=null), In(first=null, second=null)]). Without lang="en" everything is good.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AnshikaKoulcommented, Jan 24, 2018

Hi @cowtowncoder ,

My jackson version 2.9.3:

<jackson.version>2.9.3</jackson.version>
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jsr310</artifactId>
      <version>${jackson.version}</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jdk8</artifactId>
      <version>${jackson.version}</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-kotlin</artifactId>
      <version>${jackson.version}</version>
</dependency>
<dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>${jackson.version}</version>
</dependency>

Kotlin version : <kotlin.version>1.2.10</kotlin.version> Ratpack version : <ratpack.kotlin.version>1.1.2</ratpack.kotlin.version>

I too am facing an issue while deserialising an element with an attribute.

Example XML that needs to be parsed :

<Something>
    <first cn="abc">someValue</first>
    <second>3050</second>
</Something>          

classes that I am using :

data class Something(
   @JacksonXmlProperty(localName = "first", namespace = somenamespace)  val first: String
)
class ResponseXmlParser {
 private val xmlMapper = xmlObjectMapper()

  private val log = LoggerFactory.getLogger(javaClass)
  fun parseResponseXml(responseBodyStream: InputStream): Response {
    val jsonNode = xmlMapper.readValue(responseBodyStream, Envelope::class.java)
    log.info("RESPONSE: " + jsonNode.body.response)
    return jsonNode.body.response
  }

  private fun xmlObjectMapper(): XmlMapper {
    val jacksonXmlModule = JacksonXmlModule()
    jacksonXmlModule.setDefaultUseWrapper(false)

    return XmlMapper(jacksonXmlModule)
        .registerModule(KotlinModule())
        .registerModule(JavaTimeModule())
        .configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) as XmlMapper
  }
}

Now in the first element that has both attribute value and textual content. whilst parsing I get this exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.order.soap.Something (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘3050’)

which is really weird. Coz I have other elements with just attribute value and all of them work just fine. Only this element which has both attribute value and textual content doesn’t work.

1reaction
yatsykvecommented, Aug 9, 2016

OK changing fields into In.class to some XmlNode like:

@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class In {

    @JacksonXmlProperty(localName = "first")
    private XmlNode first;
    @JacksonXmlProperty(localName = "second")
    private XmlNode second;

}

And XmlNode.class :

@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class XmlNode {

    @JacksonXmlText
    private String value;
    //still ignoring any other attributes

}

solving my problems. I’m not trying it now, because previously I have very similar issue with parsing XML (https://github.com/FasterXML/jackson-dataformat-xml/issues/191). And that time XmlNode wasn’t helpful. I think it will be something similar. But in any case thank you for answer I think we can close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange behaviour in XML deserialiser - java - Stack Overflow
I'm getting some strange behaviour in Java while using SAX. Values are being set, but then reverting to their default values in the...
Read more >
Hi All, I have encounered a strange behavior with XML ...
I have encounered a strange behavior with XML Flattener. It skips some values in XML. I have flatten xml file, and some data...
Read more >
Strange behavior from AF when I export XML files - PI Square - OSIsoft
Hi. I am working with export templates to XML files from AF. However, sometimes the point configuration is not exported? Is that a...
Read more >
Unusual behavior can be used to collect a group of attributes ...
Attributes in an XMLList can be accessed using the array-access operator ([]). 4. Accesses first attribute by using E4X's attributes wildcard syntax. 5....
Read more >
Solution XML: Entity behavior attribute changing after...
Thanks for the reply Ankit but no, that wouldn't explain it - as I said there are many more sub-components than just relationships...
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