如何在使用 jaxb 进行编组时删除额外的转义字符

How to remove extra escape character while doing marshling using jaxb

原始XML amp;由 JAXB 添加,需要忽略:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <emp>
 <address>7 stret &amp; new </address>
 <name>Naveenqq</name>
</emp>

预期没有放大器;(实际值想要):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <emp>
  <address>7 stret & new </address>
  <name>Naveenqq</name>
</emp>

我试过下面的代码:

  private static void jaxbObjectToXML(Emp employee) throws IOException, SAXException, ParserConfigurationException 
{
    try
    { 

        JAXBContext jaxbContext = JAXBContext.newInstance(Emp.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        //jaxbMarshaller.setProperty("jaxb.encoding", "US-ASCII"); 
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
        //jaxbMarshaller.setProperty(OutputKeys.ENCODING, "ASCII");
        //jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
        //          jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
        //            
        //              @Override
        //              public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
        //                  out.write( ch, start, length ); 
        //                  
        //              }
        //          }); 
        //          
        //          StringWriter writer = new StringWriter();
        File file = new File("employee1.xml");
        jaxbMarshaller.marshal(employee, file); 
        //          
        //          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //          DocumentBuilder builder = factory.newDocumentBuilder();
        //          InputSource is = new InputSource( new StringReader( writer.toString() ) );
        //          Document doc = builder.parse( is );
        System.out.println("done::");


    } 
    catch (JAXBException e) 
    {
        e.printStackTrace();
    }
}

请帮忙解决同样的问题,所有编码类型我都试过了

您的预期值无效 XML,因此您无法说服任何 XML 感知工具生成它。

为什么要生成无效的 XML?

问题是 XML 中的 & 无效,如果您尝试使用 & 验证 XML,它将失败。 JAXB 非常聪明,所以它会尝试用字符实体替换特殊字符。 HTML 中也发生了类似的事情。你可以refer here.

但是如果您观察 JAXB Unmarshalling 之后的值,它已被 & 而不是 &amp; 取代。所以你不必担心它在XML。我想如果你走你想要的路线那么它会导致很多并发症并且你的 XML 本身将是无效的。

XML:

<emp>
   <address>7 stret &amp; new</address>
   <name>Naveenqq</name>
</emp>

根目录:

@Data
@XmlRootElement(name = "emp")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    private String address;
    private String name;
}

主要:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("test.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
        //marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", new XmlCharacterHandler());
        marshaller.marshal(root, System.out);
    }
}

输出:

Root(address=7 stret & new, name=Naveenqq)
<?xml version="1.0" encoding="US-ASCII"?>
<emp>
   <address>7 stret &amp; new</address>
   <name>Naveenqq</name>
</emp>

正如您在输出 Root(address=7 stret & new, name=Naveenqq) 中看到的那样,它已被 & 替换,因此您可以继续使用相同的内容。

希望解释有所帮助。