【问题标题】:How to confgure spring unmarshaller to work with list of xml如何配置 spring unmarshaller 以使用 xml 列表
【发布时间】: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


    【解决方案1】:

    如果您的项目具有类似 Maven 的结构,则应将文件夹“xmlToParse”放入 ${project.home}/src/main/resources 目录中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-21
      • 2014-09-22
      • 2015-11-28
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多