无法在骆驼中使用jaxb解组。不调用处理器

时间:2016-04-27 17:56:50

标签: java eclipse jaxb apache-camel

在我的路线中,我正在尝试解组传入的XML消息。但是,当我执行代码时,它跳过执行.processor,我无法弄清楚实际的错误(因为它没有给出一个)。

代码:

package nl.hari.local.cust;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.JaxbDataFormat;
public class XMLtoObject_Camel {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
        JAXBContext con = JAXBContext.newInstance(Address.class);   
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
            from("file://C:/Hari/TstFolder/"
                    + "?noop=true" + "&autoCreate=false" + "&flatten=false" + "&delete=false"
                    + "&bufferSize=128")
            .unmarshal(jaxbDataFormat)
//Doesn't invoke processor - start
            .process(new Processor(){
                public void process(Exchange exchange) throws Exception {
                    Address add = (Address) exchange.getIn().getBody();
                    System.out.println("city is" + add.getCity());
                }
            });
//Doesn't invoke processor - End
            }
        });
    }
}

这是我正在使用的架构。我用来测试的XML是在Eclipse中生成的,JAXB类也是如此。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.hari.nl/Address" 
        xmlns:tns="http://www.cimt.nl/Address" 
        elementFormDefault="qualified">
  <element name="address">
    <complexType>
      <sequence>
        <element type="string" name="city"/>
        <element type="string" name="country"/>
      </sequence>
      <attribute type="byte" name="id"/>
    </complexType>
  </element>
</schema>

以下链接包含项目结构的屏幕抓取 http://i.stack.imgur.com/4IWhD.png

1 个答案:

答案 0 :(得分:1)

你应该使用类似下面的代码片段

int listdir(char *dir) {
    struct dirent *dp;
    struct stat s;
    DIR *fd;
    int count = 0;

    if ((fd = opendir(dir)) == NULL) {
        fprintf(stderr, "listdir: can't open %s\n", dir);
    }
    chdir (dir); /* needed for stat to work */
    while ((dp = readdir(fd)) != NULL) {
        if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
            continue;
#ifdef _DIRENT_HAVE_D_TYPE
        switch (dp->d_type)
        {
          case DT_UNKNOWN:
            stat(dp->d_name, &s);
            if (S_ISDIR(s.st_mode)) count++;
            break;
          case DT_DIR:
            count++;
            break;
        }
#else
        stat(dp->d_name, &s);
        if (S_ISDIR(s.st_mode)) count++;
#endif            
    }
    closedir(fd);
    return count;
}

此外,在解组之前将其转换为String。

            ClassLoader cl = ObjectFactory.class.getClassLoader();
            JAXBContext jc = JAXBContext.newInstance(SomeGeneratedClass.class.getPackage().getName(), cl);
            JaxbDataFormat jaxb = new JaxbDataFormat(jc);
            jaxb.setPartClass(SomeGeneratedClass.class.getName());

最终守则应如下所示。

.convertBodyTo(String.class)
.unmarshal(jaxb)

Eclipse目录结构。

enter image description here