2
我有一個路線,駱駝驗證返回整個消息
<from uri="a">
<to uri="validator:schema.xsd">
<to uri="b">
說被驗證缺少兩個元素的XML文件,驗證似乎停止一旦它找到的第一個缺少的元素,並返回一個消息,說是失蹤。
是否有可能進行驗證XML文件來尋找其他任何缺失的元素,並返回該錯誤消息,以便發送者不必不停地發送,找出哪些元素缺失或無效?
我有一個路線,駱駝驗證返回整個消息
<from uri="a">
<to uri="validator:schema.xsd">
<to uri="b">
說被驗證缺少兩個元素的XML文件,驗證似乎停止一旦它找到的第一個缺少的元素,並返回一個消息,說是失蹤。
是否有可能進行驗證XML文件來尋找其他任何缺失的元素,並返回該錯誤消息,以便發送者不必不停地發送,找出哪些元素缺失或無效?
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>
複製我建議谷歌有關的javax測試。 xml.validation.Validator並參閱它具有哪些特性和選項來打開這種功能。以及運行在DEBUG級別的org.apache.camel.processor.validation作爲Camel DEBUG驗證器報告的每個warn/error/fatal。雖然引發的異常構造爲返回錯誤/警告列表。那麼你確定你只看到1而不是所有的錯誤? – 2013-02-15 15:42:47