You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Yong-Loh <Va...@infosys.com> on 2010/02/01 13:00:45 UTC

Only one top level element is allowed in an XML document. Error processing resource : Apache CXF with JAX-RS providers

Hi All,

We are using Apache CXF with JAX-RS to build RESTful webservices. Our
service returns a List of Content objects. Content is a XMLBeans object.

We have written a provider for converting this List of content objects into
XML. Below is the source:

@Provider 
@Produces("*/*") 

public class ContentListBodyWriter implements
MessageBodyWriter<ArrayList<Content>> 
{
        public long getSize(ArrayList<Content> t, Class<?> type, Type
genericType, 
                        Annotation[] annotations, MediaType mediaType)
        { 
            return -1; 
        } 

        public boolean isWriteable(Class<?> type, Type genericType, 
                        Annotation[] annotations, MediaType mediaType)
        { 
            return true; 
        } 

        public void writeTo(ArrayList<Content> t, Class<?> type, Type
genericType, 
                        Annotation[] annotations, MediaType mediaType, 
                        MultivaluedMap<String, Object> httpHeaders, 
                        OutputStream entityStream) throws IOException, 
                        WebApplicationException
        { 
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( 
                            entityStream)); 
            String ts = new String(); 
            Iterator<Content> i = t.iterator(); 
            while (i.hasNext()) { 
                    ts += i.next().toString(); 
            } 
            bw.write(ts); 
            bw.flush(); 
        } 


}

When we call the webservice, its giving us the below error in the browser:

Only one top level element is allowed in an XML document. Error processing
resource 

The reason for this error is, each content object is added as is. Actually,
all these content objects have to be inside one root element. Any ideas on
how to make this work so that all the content objects come under one root
element.


Thanks & Regards,
Yong-Loh
-- 
View this message in context: http://old.nabble.com/Only-one-top-level-element-is-allowed-in-an-XML-document.-Error-processing-resource-%3A-Apache-CXF-with-JAX-RS-providers-tp27403132p27403132.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Only one top level element is allowed in an XML document. Error processing resource : Apache CXF with JAX-RS providers

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

You just need to wrap the sequence yourself at the start and at the end of writeTo method

cheers, Sergey

>
> Hi All,
>
> We are using Apache CXF with JAX-RS to build RESTful webservices. Our
> service returns a List of Content objects. Content is a XMLBeans object.
>
> We have written a provider for converting this List of content objects into
> XML. Below is the source:
>
> @Provider
> @Produces("*/*")
>
> public class ContentListBodyWriter implements
> MessageBodyWriter<ArrayList<Content>>
> {
>        public long getSize(ArrayList<Content> t, Class<?> type, Type
> genericType,
>                        Annotation[] annotations, MediaType mediaType)
>        {
>            return -1;
>        }
>
>        public boolean isWriteable(Class<?> type, Type genericType,
>                        Annotation[] annotations, MediaType mediaType)
>        {
>            return true;
>        }
>
>        public void writeTo(ArrayList<Content> t, Class<?> type, Type
> genericType,
>                        Annotation[] annotations, MediaType mediaType,
>                        MultivaluedMap<String, Object> httpHeaders,
>                        OutputStream entityStream) throws IOException,
>                        WebApplicationException
>        {
>            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
>                            entityStream));
>            String ts = new String();
>            Iterator<Content> i = t.iterator();
>            while (i.hasNext()) {
>                    ts += i.next().toString();
>            }
>            bw.write(ts);
>            bw.flush();
>        }
>
>
> }
>
> When we call the webservice, its giving us the below error in the browser:
>
> Only one top level element is allowed in an XML document. Error processing
> resource
>
> The reason for this error is, each content object is added as is. Actually,
> all these content objects have to be inside one root element. Any ideas on
> how to make this work so that all the content objects come under one root
> element.
>
>
> Thanks & Regards,
> Yong-Loh
> -- 
> View this message in context: 
> http://old.nabble.com/Only-one-top-level-element-is-allowed-in-an-XML-document.-Error-processing-resource-%3A-Apache-CXF-with-JAX-RS-providers-tp27403132p27403132.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>