Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JAXB / MOXy - not unmarshalling values after ignored elements
JAXB / MOXy - not unmarshalling values after ignored elements [message #1112048] Thu, 19 September 2013 04:04 Go to next message
Chris Kirkby is currently offline Chris KirkbyFriend
Messages: 2
Registered: September 2013
Junior Member
I'm working on a JAXB/MOXy XML parsing implementation using EclipseLink v2.1.3 (old, I know but we're still tied to Java 5) and I've encountered an issue with unmarshalling.

Part of the document has the following XML:

<Product>
    <Code>code</Code>
    <Desc>description</Desc>
</ProductInfo>


The object I'm parsing this into has the following field and annotation:

@XmlElement(name = "Product/Desc")
private String description;


That is, it tries to extract the "Desc" value, but doesn't care about the "Code". I'm finding that the description field is null after unmarshalling the XML. However, if I add in a field to extract "Code":

@XmlElement(name = "Product/Code")
private String code;


then description field is not null, and contains the correct value - both values get populated.

I've found the same issue elsewhere in the XML and it seems to be that if I try to skip / ignore certain XML elements, values after that skipped elements are also ignored.

Is this a known issue, and is there a workaround for it?
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112515 is a reply to message #1112048] Thu, 19 September 2013 20:38 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Instead of use @XmlElement, you should leverage the @XmlPath annotation we introduced in EclipseLink 2.1 as follows.

Domain Model

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
    
    @XmlPath("Product/Code/text()")
    private String code;
    
    @XmlPath("Product/Desc/text()")
    private String description;

}


Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);
        
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);
        
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}


input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <Product>
      <Code>code</Code>
      <Desc>description</Desc>
   </Product>
</foo>
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112584 is a reply to message #1112515] Thu, 19 September 2013 22:57 Go to previous messageGo to next message
Chris Kirkby is currently offline Chris KirkbyFriend
Messages: 2
Registered: September 2013
Junior Member
Thanks for the response Blaise - I had tried @XmlPath as well, and it had the same behaviour.

After I'd posted this yesterday, I found that I was experiencing this bug. I've been using the following earlier in my object structure:

@XmlElement(name = ".")


Once I removed this, the issue disappeared. Our model is still in flux, so was simple enough to change how we were populating the object that used the self-element.
Re: JAXB / MOXy - not unmarshalling values after ignored elements [message #1112984 is a reply to message #1112584] Fri, 20 September 2013 13:23 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

That's great to hear that you figured out the issue. We didn't introduce a Java SE 6 dependency until EclipseLink 2.4, so you could updated to EclipseLink 2.3.3 and get the necessary bug fix:
- http://www.eclipse.org/eclipselink/downloads/previous_releases.php
Previous Topic:Implementing XMLNameTransformer breaks JSON mapping of abstract classes
Next Topic:Moxy logging configuration
Goto Forum:
  


Current Time: Thu Apr 18 10:05:56 GMT 2024

Powered by FUDForum. Page generated in 0.01973 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top