0

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

hayley
  • 45
  • 5
  • It's called an *XML declaration*, and it's not needed unless you're changing the defaults, which based on your request, you are not. Nonetheless, see the linked duplicates for how to generate an XML declaration when working with JAXB. – kjhughes Jan 19 '23 at 02:07
  • thanks for reply @kjhughes, the client want an XML declaration. I saw similar questions and fixed code, but still not working. – hayley Jan 19 '23 at 05:11
  • Don't confuse the browser default presentation of an XML file with the actual XML file. Your original question also griped about "This XML file does not appear to have any style information..." appearing at the top of the file. That's not in the file, and the XML declaration is, but is not shown. Look at the XML file in a text editor to see it as it really is. – kjhughes Jan 19 '23 at 13:13
  • Your new error message is due to your XML having an XML declaration that appears other than at the very top of the file, possibly a second XML declaration. See [here](https://stackoverflow.com/q/19889132/290085). – kjhughes Jan 19 '23 at 13:21

0 Answers0