2014-06-18 52 views
0

我想驗證java中的xml標記值。用於XML驗證的Java方法

情景: 假設我有數據如下

ELements: element1, element2, element3, element4, element5 
Values: value1, value2, value3, value4, value5, value6, value7, value8 

Now following are the possible combinations which I have to validate: 
    1. Element1 can have value1/value2 (element1->value1/value2) 
    2. Element2 can have value3/value4 (element2->value3/value4) 
    4. Element3 can have value5 if element1 has value1 (element3->value5 if element1->value1) 
    else Element3 can have value6 if element1 has value2 (element3->value6 if element1->value2) 
    5. Element4 can have value7 if element1->value1 and element2->value4 

我可以有一個Java文件的要求硬編碼,但我希望有一個靈活的方法,其中,如果在未來任何新的條件進入畫面,然後它可以很容易地添加。

我想到Hibernate驗證,但後來才知道它支持Java 6及更高版本。我的約束是我必須使用Java 1.5

請提出適當的方法來滿足上述要求。任何鏈接建議也可以工作。

注意:架構驗證已在執行中。

+1

除了Schematron之外,正如其中一個答案中所建議的那樣,您可以使用XPath規則來驗證類似於XML Schema 1.1中使用斷言的內容。 – helderdarocha

回答

0

使用DOMParser java中的類。 它會幫助你很多。