hayley
hayley

Reputation: 45

error on line 2 at column 6: XML declaration allowed only at the start of the document (JAVA)

I am making API xml using marshaller.

It works well, when I set Property(JAXB_FRAGMENT, Boolean.FALSE) but the result shows without an XML declaration.. I want to generate an XML declaration like "?xml version="1.0" encoding="utf-8"?"

I was trying to append on responseWriter, but it was not working..

When i append on StringWriter, it was working but only result printed on console not website. that is why I need to use responseWriter to display XML on Web.

If I changed (JAXB_TRAGMENT, Boolen.True), the error appear "error on line 2 at column 6: XML declaration allowed only at the start of the document"

this is result on web

enter image description here

and this is the code

@RequestMapping(value = "/sendxmltest", method = RequestMethod.GET)
    @ResponseBody
    public void xmlTesttt(HttpServletRequest request, HttpServletResponse response, YipapiVO vo) throws IOException {

        YipapiVOXML yipapiVOXML = new YipapiVOXML();
            try {
                Writer responseWriter = response.getWriter();
                JAXBContext context = JAXBContext.newInstance(YipapiVOXML.class);
                Marshaller marshaller = context.createMarshaller();

                // 보기 좋게 출력
                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
                //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
                marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                
                
                yipapiVOXML.setRespResult("TEST");
                yipapiVOXML.setErrMsg("-");
                yipapiVOXML.setTotalCnt(0);

                
                marshaller.marshal(yipapiVOXML, responseWriter);
                

            } catch (JAXBException e) {
                throw new RuntimeException("Problems generating XML in specified "
                        + "encoding, underlying problem is " + e.getMessage(),
                        e);
            }
        }

I found similar questions and trying all.. but I can't solved it

I actually don't understand why the error appear

Upvotes: 0

Views: 377

Answers (0)

Related Questions