2013-02-15 99 views
2

我有一个路线,骆驼验证返回整个消息

<from uri="a"> 
<to uri="validator:schema.xsd"> 
<to uri="b"> 

说被验证缺少两个元素的XML文件,验证似乎停止一旦它找到的第一个缺少的元素,并返回一个消息,说是失踪。

是否有可能进行验证XML文件来寻找其他任何缺失的元素,并返回该错误消息,以便发送者不必不停地发送,找出哪些元素缺失或无效?

+2

复制我建议谷歌有关的javax测试。 xml.validation.Validator并参阅它具有哪些特性和选项来打开这种功能。以及运行在DEBUG级别的org.apache.camel.processor.validation作为Camel DEBUG验证器报告的每个warn/error/fatal。虽然引发的异常构造为返回错误/警告列表。那么你确定你只看到1而不是所有的错误? – 2013-02-15 15:42:47

回答

0

Validator component抛出SchemaValidationException,如果验证失败。此例外包含返回List<org.xml.sax.SAXParseException>的方法getError()。 您可以使用它在onException块中将消息转换为List<org.xml.sax.SAXParseException>

以下代码捕获SchemaValidationException,将正文转换为SchemaValidationException.getErrors()并将异常标记为continued,以继续路由并在输出路由上返回此列表。

from("timer:simple?period=1000") 
    .setBody(constant(XML_INVALID)) 
    .to("direct:a"); 

from("direct:a") 
    .onException(SchemaValidationException.class) 
     .to("direct:validationErrors") 
     .continued(true) 
     .end() 
    .to("validator:test.xsd") 
    .to("log:result"); 

from("direct:validationErrors") 
    .setBody(simple("${property.CamelExceptionCaught.errors}")) 
    .end(); 

注:此示例与以下资源

XML_INVALID

<?xml version="1.0" encoding="utf-8"?> 
<shiporder orderid="str1234"> 
    <orderperson>str1234</orderperson> 
    <shipto> 
    <name>str1234</name> 
    <address>str1234</address> 
    </shipto> 
    <item> 
    <quantity>745</quantity> 
    <price>123.45</price> 
    </item> 
</shiporder> 

test.xsd从w3schools.com

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<!-- definition of simple elements --> 
<xs:element name="orderperson" type="xs:string"/> 
<xs:element name="name" type="xs:string"/> 
<xs:element name="address" type="xs:string"/> 
<xs:element name="city" type="xs:string"/> 
<xs:element name="country" type="xs:string"/> 
<xs:element name="title" type="xs:string"/> 
<xs:element name="note" type="xs:string"/> 
<xs:element name="quantity" type="xs:positiveInteger"/> 
<xs:element name="price" type="xs:decimal"/> 

<!-- definition of attributes --> 
<xs:attribute name="orderid" type="xs:string"/> 

<!-- definition of complex elements --> 
<xs:element name="shipto"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="name"/> 
      <xs:element ref="address"/> 
      <xs:element ref="city"/> 
      <xs:element ref="country"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="item"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="title"/> 
      <xs:element ref="note" minOccurs="0"/> 
      <xs:element ref="quantity"/> 
      <xs:element ref="price"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="shiporder"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="orderperson"/> 
      <xs:element ref="shipto"/> 
      <xs:element ref="item" maxOccurs="unbounded"/> 
     </xs:sequence> 
     <xs:attribute ref="orderid" use="required"/> 
    </xs:complexType> 
</xs:element> 

</xs:schema>