开发者

Handling empty xml tags using java

i have an xml as below.

<stat>
    <Parents>
        <Parent>
            <parentName></parentName>
            <occupation></occupation>
        </Parent>
        <Parent>
            <parentName>Brian</parentName>
            <occupation>Doctor</occupation>
        </Parent>
    </Parents>
     <Parents/>
    <Parents>
        <Parent>
            <parentName></parentName>
            <occupation></occupation开发者_如何学编程>
        </Parent>
        <Parent/>
    </Parents>
</stat>

I need to validate the xml file using java in based on the parents values.When ever the xml file contains empty parents block for e.g: <Parents/> or <Parents> <Parent><parentName></parentName><occupation></occupation></Parent></Parents>.I need to throw an exception saying xml is not valid.How to implement the requirment using either xpath or some approach with out performance issue. Your help appreciated.


First, I'd recommend you to take a look on XSD. Using XSD you can define which tags and how many such tags may appear in your XML. Once you are done just validate XML: http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPDOM8.html

d


Aside from XSD or RelaxNG schemas (which should allow specifying acceptable models; and empty one not being one of them, can report it), this would be easy to deal with Stax XMLStreamReader (javax.xml.stream.) as well. Read any tutorial, and check for START_ELEMENT/END_ELEMENT pairs (with 'parents' as local name).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜