简体   繁体   English

在Java 9中如何在没有外部库的情况下创建XML文件?

[英]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.JAXBContextjavax.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兼容。


The way I am creating XML file right now is: 我现在创建XML文件的方式是:

1. Create XML element class 1.创建XML元素类

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);

2. Create XML file 2.创建XML文件

 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); 


I hope someone encountered with this problem already and know how to solve it. 我希望有人已经遇到了这个问题,并且知道如何解决。

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.javajava.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模块提供更多功能,因此requiresmodule-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.

相关问题 如何将 apache.commons.lang3 添加到 Jhipster 外部库而不添加到 pom.xml 文件? - How apache.commons.lang3 is added to Jhipster external libraries without adding to pom.xml file? 有没有办法在没有外部库的情况下用Java处理DBF文件? - Is there a way to process a DBF file in Java without external libraries? Java如何在不使用外部库的情况下从毫秒获取月份 - Java How to get Month from milliseconds WITHOUT using external Libraries JavaFX 在没有外部库的情况下创建弹出窗口 - JavaFX create popover without external libraries 在没有外部库的情况下解析Java中的XLSX文件? - Parse XLSX files in Java without external libraries? 无需外部库就位过滤Java列表 - Filter Java List in place without external libraries Eclipse使用外部库创建Java可执行文件 - Eclipse create java executable with external libraries java.xml.bind从外部文件创建XMLStreamReader - java.xml.bind create XMLStreamReader from an external file 在使用ant build.xml进行编译时,如何在没有JDK库的情况下使用外部库 - How to use external library without JDK libraries while compiling using ant build.xml ClassNotFoundException - 如何在没有 Maven 的情况下使用外部库? - ClassNotFoundException - How to use external libraries without maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM