eclipse-ee4j / jaxb-ri

Jaxb RI
https://eclipse-ee4j.github.io/jaxb-ri/
BSD 3-Clause "New" or "Revised" License
205 stars 111 forks source link

Unmarshalling Issues #1791

Open bhyatm opened 8 months ago

bhyatm commented 8 months ago

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 commented 8 months ago

@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.