[英]How to create XML file without external libraries in Java 9?
How to create
XML
file without external libraries in Java 9? 在Java 9中如何在没有外部库的情况下创建XML
文件?
Currently I am using javax.xml.bind.JAXBContext
and javax.xml.bind.Marshaller
classes to do that, but on Java 9 these classes does not exist any more. 当前,我正在使用javax.xml.bind.JAXBContext
和javax.xml.bind.Marshaller
类来执行此操作,但是在Java 9上这些类不再存在。 So while using Java 9 I am getting an exception: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
. 因此,在使用Java 9时,我遇到一个异常: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
。
I can create XML
file with lower Java version, but I need it will be compatible and for Java 9. 我可以使用较低的Java版本创建XML
文件,但我需要它与Java 9兼容。
List<Athlete> athletes = new ArrayList<>();
Places places = new Places();
places.setAthletes(athletes);
JAXBContext context = JAXBContext.newInstance(Places.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(places, new File(outputPath);
marshaller.marshal(places, System.out);
List<Athlete> athletes = new ArrayList<>(); Places places = new Places(); places.setAthletes(athletes); JAXBContext context = JAXBContext.newInstance(Places.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(places, new File(outputPath); marshaller.marshal(places, System.out);
PS: I have searched for the answer of this question but doesn't found anything, so it shouldn't be a duplicated question. PS: 我已经搜索了该问题的答案,但未找到任何内容,因此它不应是重复的问题。
As I mention I have to use only Java internal libraries for that. 正如我提到的那样, 我只需要使用Java内部库即可。
You can use these libraries in Java 9, but you need to require the module that contains them. 您可以在Java 9中使用这些库,但是需要包含它们的模块。 Ensure that your module-info.java
has a requirement for the java.xml
module: 确保您的module-info.java
对java.xml
模块有要求:
module myModule {
requires java.xml;
}
As you require more functionality from the JAXB modules, you will need to add more requires
statements in your module-info.java
file. 由于需要JAXB模块提供更多功能,因此requires
在module-info.java
文件中添加更多module-info.java
语句。 For example, your module-info.java
file may look more like the following when you are done: 例如,完成module-info.java
,您的module-info.java
文件可能看起来更像以下内容:
module myModule {
java.xml
java.xml.bind
java.xml.ws
java.xml.ws.annotation
}
For more information, see How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
in Java 9 . 有关更多信息,请参见如何解决Java 9中的java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.