返回顶部

收藏

JAXB marshling

更多

使用JAXB将对象转成XML字符串

Play.java

public class Play {
    public static void generateXML(Object rootElement,
        Writer writer) throws JAXBException {

        if (rootElement == null)
          throw new IllegalArgumentException("rootElement is null");
        if (writer == null)
          throw new IllegalArgumentException("writer is null");

        JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(
          Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(rootElement, writer);
      }

    public static void main(String[] args) throws JAXBException {
        final StringWriter writer = new StringWriter();
        generateXML(new AnnotatedClass(), writer);
        System.out.println( writer.getBuffer().toString());
    }

}

标签:marshling,jQuery,XML,JAXBContext,JAXB

收藏

0人收藏

支持

0

反对

0

发表评论