【发布时间】:2016-04-04 17:59:49
【问题描述】:
我想在实体字段中解析我的 xml 文件和过去。
在我的 Initializer.class 中,我有:
private void parseData() {
ApplicationContext appContext = new ClassPathXmlApplicationContext("parser.xml");
XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter");
//from XML to object
//Company is an entity
Company company = null;
try {
company = (Company)converter.convertFromXMLToObject(XML_FILE_NAME);
} catch (IOException e) {
log.error(e);
}
log.info(company);
log.info("done");
}
XML转换器:
public class XMLConverter {
private Unmarshaller unmarshaller;
private static final Logger log = Logger.getLogger(XMLConverter.class);
public Unmarshaller getUnmarshaller() {
return unmarshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
URL url = this.getClass().getResource("/xmlToParse/companies.xml");
File file = new File(url.getPath());
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}}
Parser.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="XMLConverter" class="server.XMLConverter">
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
但它找不到我的文件夹“xmlToParse”(也许我没有 bean?)。
如何配置解组器权限。请帮帮我。
【问题讨论】:
标签: java xml spring jboss unmarshalling