私はしばらくの間JAXBを使用して、おおよそ次のようなxmlを解析しています。
<report> <-- corresponds to a "wrapper" object that holds
some properties and two lists - a list of A's and list of B's
<some tags with> general <info/>
...
<A> <-- corresponds to an "A" object with some properties
<some tags with> info related to the <A> tag <bla/>
...
<A/>
<B> <-- corresponds to an "B" object with some properties
<some tags with> info related to the <B> tag <bla/>
...
</B>
</report>
xmlのマーシャリングを担当する側はひどいですが、私のコントロールの外にあります。
多くの場合、無効なxml文字や不正な形式のxmlを送信します。
私は責任者に相談し、多くのエラーを修正しましたが、一部のエラーは修正できないようです。
パーサーがこれらのエラーに対して可能な限り寛容であり、それが不可能な場合は、エラーのあるxmlから可能な限り多くの情報を取得することを望んでいます。
したがって、xmlに100個のAが含まれていて、1つに問題がある場合でも、他の99個を保持できるようにしたいと思います。
これらは私の最も一般的な問題です。
1. Some info tag inner value contains invalid chars
<bla> invalid chars here, either control chars or just &>< </bla>
2. The root entity is missing a closing tag
<report> ..... stuff here .... NO </report> at the end!
3. An inner entity (A/B) is missing it's closing tag, or it's somehow malformed.
<A> ...stuff here... <somethingMalformed_blabla_A/>
OR
<A> ... Something malformed here...</A>
私は自分自身をうまく説明したいと思いました。
問題が発生した場合でも、これらのxmlからできるだけ多くの情報を取得したいと思います。
JAXBと一緒にstax/saxを使用する戦略を採用する必要があると思いますが、その方法はわかりません。
100個のAの場合、1個のAにxmlの問題があり、そのAだけをスローしてもかまいません
。エラーが発生するまで解析できるデータをできるだけ多く含むAオブジェクトを取得できれば、はるかに良いでしょう。