从InputStream解组

杰库马尔

我已经写了简单的对象给马歇尔。我将其编组到StringWriter一个文件而不是文件中。它工作正常,但我不确定如何解组。

这是我到目前为止编写的代码。

public static void main(String[] args) throws JAXBException, FileNotFoundException {
    JAXBContext context = JAXBContext.newInstance(Employee.class);
    Marshaller marshaller = context.createMarshaller();

    Employee employee = new Employee();
    employee.setId(1002);
    employee.setOccupation("Software Developer");
    employee.setSalary("56000");

    StringWriter writer = new StringWriter();

    marshaller.marshal(employee, writer);
    System.out.println("Object Marshalled Successfully");

    Conversion.unMarshallingObject();
}

public static void unMarshallingObject() throws JAXBException {
    StringWriter writer = new StringWriter();
    InputStream stream = new ByteArrayInputStream(writer.toString().getBytes());

    JAXBContext context = JAXBContext.newInstance(Employee.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    Employee employee = (Employee) unmarshaller.unmarshal(stream);

    System.out.println("Object unmarshalled successfully");
    System.out.println("**********************************");
    System.out.println(employee.getId() + " " + employee.getOccupation() + " " + employee.getSalary());

} 

有很多示例,但所有示例都传递一个文件。我不要任何文件。有可能这样做吗?

谢谢

用户名

根据文档,您可以取消Reader罚款-其中包括StringReader

 Employee employee = (Employee) unmarshaller.unmarshal(new StringReader(yourString));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章