Java 如何配置spring解组器以使用xml列表
我想在entities字段中解析我的xml文件和过去 在Initializer.class中,我有:Java 如何配置spring解组器以使用xml列表,java,xml,spring,jboss,unmarshalling,Java,Xml,Spring,Jboss,Unmarshalling,我想在entities字段中解析我的xml文件和过去 在Initializer.class中,我有: private void parseData() { ApplicationContext appContext = new ClassPathXmlApplicationContext("parser.xml"); XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter");
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");
}
XMLConverter:
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?)
如何配置解组器权限。请帮帮我 如果您的项目具有类似Maven的结构,那么文件夹“xmlToParse”应该放在${project.home}/src/main/resources目录中