2

ルートがありますが、

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

検証中の XML ファイルに 2 つの要素が欠落しているとします。最初の欠落している要素が見つかると、バリデーターは停止したように見え、それが欠落しているというメッセージを返します。

XML ファイルの検証を続行して、欠落している他の要素を探し、それをエラー メッセージで返すことは可能ですか?送信者は、欠落している要素や無効な要素を見つけるために送信し続ける必要はありませんか?

4

1 に答える 1

0

Validator コンポーネントは、検証が失敗した場合にSchemaValidationExceptionをスローします。この例外には、 をgetError()返すメソッドが含まれていますList<org.xml.sax.SAXParseException>。これを使用して、メッセージをList<org.xml.sax.SAXParseException>onException ブロックに変換できます。

次のコードは 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_無効

<?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>

w3schools.com からコピーしたtest.xsd

<?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>
于 2017-12-02T13:09:48.263 に答える