如何在给定的xml上执行解组(预留工程)

时间:2015-09-22 23:31:30

标签: java marshalling unmarshalling java-5

Down下面我有一个方法来处理给定Serializable事件的编组。我想要实现的是获取此方法的String结果,然后重新创建或重建事件对象,我不知道如果这可以是使用Unmarshaller完成。你能不能请求

private String getTextOutOfSerializable(Serializable event)  {
        String myXmlString          = StringUtils.EMPTY;

        try {

             StringWriter stringWriter   = new StringWriter();
             Marshaller marshaller       = null;

             marshaller = new Marshaller(stringWriter);
             marshaller.setValidation(false);
             marshaller.marshal(event);
             myXmlString = stringWriter.toString();
        }catch(IOException e1){
            LOGGER.error(" getTextOutOfSerializable IOException  ",e1);
        }catch(MarshalException e2){
            LOGGER.error(" getTextOutOfSerializable MarshalException ",e2);
        }catch(ValidationException e3){
            LOGGER.error(" getTextOutOfSerializable ValidationException ",e3);
        }


        return myXmlString;
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用JAXB。致Marshal:

 StringWriter sw = new StringWriter();
 JAXB.marshal(javaObject, sw);

对于解组使用:

  JavaObject unmarshal = JAXB.unmarshal(new StringReader(string), JavObject.class);
  System.out.println(unmarshal.property);