2016-04-04 14 views
0

xmlファイルと過去のエンティティフィールドを解析したいと思います。私Initializer.classでspring unmarshallerとxmlのリストを連携させる方法

、私が持っている:

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"(多分私didnそれじゃない?)。

unmarshallerを正しく設定する方法。お願い助けて。

答えて

0

プロジェクトにMavenのような構造がある場合は、 "xmlToParse"というフォルダを$ {project.home}/src/main/resourcesディレクトリに配置する必要があります。

関連する問題