JAXB 去掉xml header
要从JAXB生成的XML中删除头部,可以通过以下步骤进行:
- 创建一个JAXBContext对象并设置Marshaller的属性,将其格式化为不包含XML头的格式:
JAXBContext context = JAXBContext.newInstance(MyClass.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
- 使用Marshaller将对象转换为字符串并输出,这将不包括XML头:
MyClass myClass = new MyClass();
StringWriter writer = new StringWriter();
marshaller.marshal(myClass, writer);
System.out.println(writer.toString());
这将输出一个不包含XML头的XML字符串。
原文地址: https://www.cveoy.top/t/topic/bjnP 著作权归作者所有。请勿转载和采集!