Java XML到对象转换过程(xstream除外)
我使用的是xstream api,如下所示,但现在请告知,除了java中的xstream(如JAXB)之外,我还可以使用api实现同样的xml到java对象转换过程吗。如果可能的话,请转告我,除了使用xstream之外,我如何转换此文件 假设我们需要从xml文件加载配置:Java XML到对象转换过程(xstream除外),java,xml-parsing,Java,Xml Parsing,我使用的是xstream api,如下所示,但现在请告知,除了java中的xstream(如JAXB)之外,我还可以使用api实现同样的xml到java对象转换过程吗。如果可能的话,请转告我,除了使用xstream之外,我如何转换此文件 假设我们需要从xml文件加载配置: 01 <config> 02 <inputFile>/Users/tomek/work/mystuff/input.csv</inputFile> 03 <tru
01 <config>
02 <inputFile>/Users/tomek/work/mystuff/input.csv</inputFile>
03 <truststoreFile>/Users/tomek/work/mystuff/truststore.ts</truststoreFile>
04 <keystoreFile>/Users/tomek/work/mystuff/CN-user.jks</keystoreFile>
05
06 <!-- ssl stores passwords-->
07 <truststorePassword>password</truststorePassword>
08 <keystorePassword>password</keystorePassword>
09
10 <!-- user credentials -->
11 <user>user</user>
12 <password>secret</password>
13 </config>
所以基本上我们要做的是:
1 FileReader fileReader = new FileReader("config.xml"); // load our xml file
2 XStream xstream = new XStream(); // init XStream
3 // define root alias so XStream knows which element and which class are equivalent
4 xstream.alias("config", Configuration.class);
5 Configuration loadedConfig = (Configuration) xstream.fromXML(fileReader);
杰克逊在这方面做得很好。最基本的是,您可以简单地执行以下操作:
XmlMapper mapper = new XmlMapper();
mapper.writeValue(myFile, myObject)
以下是如何使用。JAXB的实现包含在JavaSE6及更高版本中 Java模型(
配置)
)
JAXB不需要任何注释(请参见:),但是将根元素映射到@XmlRootElement
会使事情变得更简单。默认情况下,JAXB将从公共属性派生映射,但我使用了@xmlacessortype(xmlacesstype.FIELD)
,因此我可以排除它们,以便发布一个较小的工作类(请参阅:)
演示代码(Demo
)
下面的演示代码将XML转换为对象形式,然后将其写回XML
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Configuration.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum19407064/input.xml");
Configuration configuration = (Configuration) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(configuration, System.out);
}
}
其他信息
因为您熟悉XStream,所以这里有一篇我写的文章,它将对象模型与JAXB和XStream进行映射,以了解它们之间的区别
import javax.xml.bind.annotation.*;
@XmlRootElement(name="config")
@XmlAccessorType(XmlAccessType.FIELD)
public class Configuration {
private String inputFile;
private String user;
private String password;
private String truststoreFile;
private String keystoreFile;
private String keystorePassword;
private String truststorePassword;
// getters, setters, etc.
}
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Configuration.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum19407064/input.xml");
Configuration configuration = (Configuration) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(configuration, System.out);
}
}