Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将XML解组到对象(Apache Camel)_Java_Xml_Apache Camel - Fatal编程技术网

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

在Java中将XML解组到对象(Apache Camel),java,xml,apache-camel,Java,Xml,Apache Camel,我有一个非常简单的程序,可以用今天的货币从互联网上获取XML文件。我想将这个XML文件的内容转换为Java对象,以便以后使用它 我尝试转换的XML文件如下所示: 当我运行程序时,出现以下错误: mvn清理编译骆驼:运行 原因:org.apache.camel.runtimecamel异常: org.apache.camel.FailedToCreateRouteException:未能创建路由 route2:Route(route2)[[From[direct:readFile]]->[Log[

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

我尝试转换的XML文件如下所示:

当我运行程序时,出现以下错误:

mvn清理编译骆驼:运行

原因:org.apache.camel.runtimecamel异常: org.apache.camel.FailedToCreateRouteException:未能创建路由 route2:Route(route2)[[From[direct:readFile]]->[Log[####Read file] …由于提供程序的原因,com.sun.xml.bind.v2.ContextFactory无法 要实例化:javax.xml.bind.JAXBException: “com.domain.subdomain.domain”不包含ObjectFactory.class或 jaxb.index

原因:javax.xml.bind.JAXBException:“com.domain.subdomain.domain” 不包含ObjectFactory.class或jaxb.index

下面是我的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>
<?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/domain/subdomain/domain/Currency.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;
    }
}
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;
    }
}

尝试将上下文设置为缺少上下文,请参见下面的异常…参考代码

@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")
        ....
        ....

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

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();
}

您也可以在上查看我的存档文件,以获得更好的示例。

您的jaxb包(com.domain.subdomain.domain)是否包含所需的ObjectFactory类?如果您自己创建了绑定,则可能不是这种情况。您使用的是哪个java版本?我不确定ObjectFactory类是什么。是否需要创建它?我使用的是java 8。这给了我一个错误:无法解析方法“setContext”javax.xml.bind.JAXBContext查看JAXBContext.newInstance的规范(…这里的内容…)。我的代码仅供参考,如果您提供正确的输入,应该可以使用。