首页 > 解决方案 > 如何在使用 jaxb 进行编组时删除额外的转义字符

问题描述

原始 XML 放大器;由需要忽略的 JAXB 添加:-

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

预期没有 amp;(实际值需要):

<?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();
    }
}

请帮助如何解决相同的问题,我已经尝试了所有编码类型

标签: javaxmljaxb

解决方案


问题是&XML 无效,如果您尝试使用&它验证 XML 将失败。JAXB非常聪明,因此它会尝试用它们的字符实体替换特殊字符。类似的事情也发生在 HTML 中。你可以参考这里。

但是,如果您在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)它已被替换,&因此您可以继续使用它。

希望解释有所帮助。


推荐阅读