在编组之前,我是否需要传入所有类以在JAXBContext中绑定

时间:2016-10-06 08:17:12

标签: java xml class jaxb

我正在开发一个项目,我需要从服务器检索一个json对象并将其转换为xml格式。

现在,它目前工作正常,但我想确保我以正确的方式做到这一点。

我有这些应该绑定的java类。

public class StrutsAction {

public execute() {
    //initial logic

    JAXBContext jaxbContext = JAXBContext.newInstance(Response.class, Message.class, Case.class);
    Marshaller marshaller = jaxbContext.createMarshaller(); 
    StringWriter sw = new StringWriter();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(reponse, sw);
    return new  ByteArrayInputStream(sw.toString().getBytes("UTF-8"));
}
}

现在这就是我在我的动作类中所做的工作正常:请注意我传入了我想要绑定的所有类。

public class StrutsAction {

public execute() {
    //initial logic

    JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
    Marshaller marshaller = jaxbContext.createMarshaller(); 
    StringWriter sw = new StringWriter();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(reponse, sw);
    return new  ByteArrayInputStream(sw.toString().getBytes("UTF-8"));
}
}

我尝试这样做,它仍然有效:这里我只是在Response.class中传递了

public static InputStream generateSuccessMsgResponse(Object object, Class<?>...classes) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(classes);
        Marshaller marshaller = jaxbContext.createMarshaller(); 
        StringWriter sw = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, sw);
        return new ByteArrayInputStream(sw.toString().getBytes("UTF-8"));
    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

现在,在我的实际项目中,我有更多要绑定的类,如果可能的话,当我调用JAXBContext.newInstance(class ...)时,我不想传递所有类。那么有人可以告诉我最好的方法。我什么时候应该传入所有要绑定的类。我还没有完全理解文档上的内容。

我添加了这个帮助方法,它生成响应以使我的代码更清洁:

public String execute() {
inputStream = MethodUtil.generateSuccessMsgResponse(ResponseObj, Response.class, Message.class, Case.class, OtherClass.class, AnotherClass.class);                                                                                                                                                                                                                                                                                                                                                              
}

但是每当我调用它时,我仍然需要传递所有绑定类:

$sql="SELECT `state`, `district`, `postoffice`, count(`district`) as totcount FROM `tbl_pincodes` WHERE `state`='$state' group by `district`";

所以我真正的问题是我必须传递所有这些课程吗?因为如果我只是传入Response.class,它的工作方式是一样的。

1 个答案:

答案 0 :(得分:0)

您可以获取对象的类并将其传递给JAXBConext。像这样:

    public static String unparse(Object object, String encoding) throws JAXBException, SQLException {
        JAXBContext jc = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);        
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        return writer.toString();
    }

用法:

    public void execute() { 
        String messageXml = unparse(messageObject, "UTF-8");
        String responseXml = unparse(responseObject, "UTF-8");
        String caseXml = unparse(caseObject, "UTF-8");
    }