在Java中解组XML到对象(Apache Camel)

时间:2018-05-22 09:56:28

标签: java xml apache-camel

我有一个非常简单的程序,可以使用今天的货币从Internet获取XML文件。我想将这个XML文件的内容转换为Java对象,以后我可以使用它。

我要转换的XML文件位于:https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml

当我运行程序时,我收到此错误:

  

mvn clean compile camel:run

     

引起:org.apache.camel.RuntimeCamelException:   org.apache.camel.FailedToCreateRouteException:无法创建路由   route2:Route(route2)[[from [direct:readFile]] - > [Log [###读取文件   ...因为提供者com.sun.xml.bind.v2.ContextFactory不能   实例化:javax.xml.bind.JAXBException:   “com.domain.subdomain.domain”不包含ObjectFactory.class或   jaxb.in​​dex

     

引起:javax.xml.bind.JAXBException:“com.domain.subdomain.domain”   不包含ObjectFactory.class或jaxb.in​​dex

以下是我的Java文件。

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.domain.subdomain</groupId>
    <artifactId>xml-to-object</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>spi-annotations</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz</artifactId>
            <version>2.20.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jaxb -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jaxb</artifactId>
            <version>2.20.2</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- Allows the routes to be run via 'mvn camel:run' -->
            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>2.20.0</version>
            </plugin>
        </plugins>
    </build>
</project>

资源/ META-INF /弹簧/骆驼contxt.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="CurrencyRoute" class="com.domain.subdomain.routes.CurrencyRoute">
        <property name="currencyWsURL" value="www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml" />
    </bean>

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="CurrencyRoute"/>
    </camelContext>

</beans>

的java / COM /域/子域/ CurrencyRoute.java:

package com.domain.subdomain.routes;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.spi.DataFormat;


public class CurrencyRoute extends RouteBuilder {


    private String currencyWsURL;

    @Override
    public void configure() {
        DataFormat jaxbDataFormat = new JaxbDataFormat("com.domain.subdomain.domain");


        from("quartz://myTimer?trigger.repeatCount=0")
                .log("### Quartz trigger ###")
                .to("direct:readFile");

        from("direct:readFile")
                .log("### Read file ###")
                .to("https4://" + currencyWsURL)
                .unmarshal(jaxbDataFormat)
                .log("${body}");
    }

    public void setCurrencyWsURL(String currencyWsURL) {
        this.currencyWsURL = currencyWsURL;
    }
}

的java / COM /域/子域/域名/ Currency.java:

package com.domain.subdomain.domain;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "valuta")
@XmlAccessorType(XmlAccessType.FIELD)
public class Currency {

    @XmlElement(name = "land")
    String country;

    @XmlElement(name = "isokode")
    String isoCode;

    @XmlElement(name = "kode")
    String code;

    @XmlElement(name = "enhet")
    Double unit;

    @XmlElement(name = "navn")
    String name;

    @XmlElement(name = "kjop")
    Double transferPurcase;

    @XmlElement(name = "salg")
    Double transferSell;

    @XmlElement(name = "endring")
    Double transferChanges;

    @XmlElement(name = "forrige")
    Double transferPrevious;

    @XmlElement(name = "midtkurs")
    Double transferMiddleCourse;

    @XmlElement(name = "kjop")
    Double notePurcase;

    @XmlElement(name = "salg")
    Double noteSell;



    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Double getUnit() {
        return unit;
    }

    public void setUnit(Double unit) {
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getTransferPurcase() {
        return transferPurcase;
    }

    public void setTransferPurcase(Double transferPurcase) {
        this.transferPurcase = transferPurcase;
    }

    public Double getTransferSell() {
        return transferSell;
    }

    public void setTransferSell(Double transferSell) {
        this.transferSell = transferSell;
    }

    public Double getTransferChanges() {
        return transferChanges;
    }

    public void setTransferChanges(Double transferChanges) {
        this.transferChanges = transferChanges;
    }

    public Double getTransferPrevious() {
        return transferPrevious;
    }

    public void setTransferPrevious(Double transferPrevious) {
        this.transferPrevious = transferPrevious;
    }

    public Double getTransferMiddleCourse() {
        return transferMiddleCourse;
    }

    public void setTransferMiddleCourse(Double transferMiddleCourse) {
        this.transferMiddleCourse = transferMiddleCourse;
    }

    public Double getNotePurcase() {
        return notePurcase;
    }

    public void setNotePurcase(Double notePurcase) {
        this.notePurcase = notePurcase;
    }

    public Double getNoteSell() {
        return noteSell;
    }

    public void setNoteSell(Double noteSell) {
        this.noteSell = noteSell;
    }
}

2 个答案:

答案 0 :(得分:0)

根据例外情况尝试将上下文设置为缺失...参考代码如下。

@Override 
            public void configure() throws Exception { 

                JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(); 
        // add path to class - 
                jaxbDataFormat.setContext(JAXBContext.newInstance(com.domain.subdomain.domain.Currency.class));
                 from("quartz://myTimer?trigger.repeatCount=0")
        ....
        ....

答案 1 :(得分:0)

这可能是您实施路线的方式的问题。下面的这个例子对我来说很好用:

private static final String INBOX = "file:data/inbox?noop=true&include=.*.xml";

@Override
public void configure() throws Exception {

    JAXBContext context = JAXBContext.newInstance(new Class[]{io.tiago.feed.Animals.class});
    JaxbDataFormat xmlDataFormat = new JaxbDataFormat();
    xmlDataFormat.setContext(context);

    from(INBOX).doTry().unmarshal(xmlDataFormat)
            .split().tokenizeXML("status")
            // IF YOUR PAYLOAD IS BIG, YOU MAY WANT TO SPLIT IT IN STREAMING MODE WHICH MEANS IT WILL SPLIT THE INPUT MESSAGE IN CHUNKS
            .streaming()
            .to("file://data/outbox")
            .end();
}

您也可以在 https://matplotlib.org/devdocs/api/figure_api.html#matplotlib.figure.FigureBase.supxlabel 查看我的档案以获得更好的示例。