Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JAXB将Java转换为XML_Java_Xml_Web Services_Jaxb - Fatal编程技术网

使用JAXB将Java转换为XML

使用JAXB将Java转换为XML,java,xml,web-services,jaxb,Java,Xml,Web Services,Jaxb,我正在使用JAXB将Java对象打包成XML 我想创建一个web服务,在其中输入代码并返回xml,但最后一行marshall并没有返回字符串,因为它是一个处理程序 JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORM

我正在使用JAXB将Java对象打包成XML

我想创建一个web服务,在其中输入代码并返回xml,但最后一行marshall并没有返回字符串,因为它是一个处理程序

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Person person = new Person("Anonymous", 32, "employee");    

    marshaller.marshal(person, System.out);

以前有人这样做过吗?

您可以将其封送到StringWriter。

marshaller.marshall()有几个重写的方法,允许您将对象封送到输出流、文件、w3c.dom.Node。。。等等

更好的问题可能是您使用什么编写web服务。如果您使用的是JAX-WS,那么这些事情将自动为您完成


只是一个注释,JAXBContext.newInstance需要缓存,因为它可能非常昂贵


是的,它是JAX-WS,所以我要看一看。Web服务不会返回编组为XML的Java对象。返回以下内容:− − Webservice JAXB@bruno-StringWriter是Writer的一个实现。