Can I force JAXB not to convert " into ", for example, when marshalling to XML?

后端 未结 14 1997
情深已故
情深已故 2020-11-29 09:27

I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes (\"). The resulting XML has " where t

14条回答
  •  甜味超标
    2020-11-29 09:36

    i found same issue i fixed this using xmlWriter in xmlWriter file there is one method isEscapeText() and setEscapeTest that is by default true if you dont want transformation between < to < that time you need to setEscapeTest(false); during marshalling

    JAXBContext jaxbContext = JAXBContext.newInstance(your class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
    // Create a filter that will remove the xmlns attribute
    NamespaceFilter outFilter = new NamespaceFilter(null, false);
    
    // Do some formatting, this is obviously optional and may effect
    // performance
    OutputFormat format = new OutputFormat();
    format.setIndent(true);
    format.setNewlines(true);
    
    // Create a new org.dom4j.io.XMLWriter that will serve as the
    // ContentHandler for our filter.
    XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
    writer.setEscapeText(false); // <----------------- this line
    // Attach the writer to the filter
    outFilter.setContentHandler(writer);
    // marshalling
    marshaller.marshal(piaDto, outFilter);
    marshaller.marshal(piaDto, System.out);
    

    this change writer.setEscapeText(false); fixed my issue hope this changes helpful to you

提交回复
热议问题