Skip to content
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

Unmarshalling Issues #1791

Open
bhyatm opened this issue Feb 22, 2024 · 1 comment
Open

Unmarshalling Issues #1791

bhyatm opened this issue Feb 22, 2024 · 1 comment

Comments

@bhyatm
Copy link

bhyatm commented Feb 22, 2024

Hi,

I am having an issue with the UnMarshallerImpl

I have a simple XML String which I am trying to convert to POJO

         StringReader reader = new StringReader(EmployeeXML);
        StreamSource source1 = new StreamSource(reader);
        jaxbContext = JAXBContext.newInstance(Employee.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return jaxbUnmarshaller.unmarshal(source1, c).getValue();

However everytime I do I get a stream is closed IOException

image

I have narrowed it down to the the logic in the UnmarshallerImp which converts the Incoming Stream Source into an InputSource

The streamSourceToInputSource code works fine, but the output of that when returned set the stringreader str to null (somehow) and that results in in the error

I am using OpenJDK 21.0.2 on a mac with runtime version 4.0.1 (latest)

anyone had this issue ?

@antoniosanct
Copy link
Contributor

antoniosanct commented Feb 22, 2024

@bhyatm
Could you attach sample XML and Employee structure class? What does it mean 'c'?
I wrote a sample code in jaxb-runtime test class:

private static JAXBContext context;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        context = JAXBContext.newInstance(Employee.class);
    }

    @Test
    public void unmarshallEmployee() throws JAXBException {
        StringReader reader = new StringReader("<employee><name>bhyatm</name></employee>");
        StreamSource source1 = new StreamSource(reader);
        Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
        Employee emp = jaxbUnmarshaller.unmarshal(source1, Employee.class).getValue();
        Assert.assertEquals("bhyatm", emp.getName());
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "employee")
    private static class Employee {

        @XmlElement
        private String name;
        
        public String getName() {
            return this.name;
        }
    }

Regards,
Antonio.

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

2 participants