how to detect the duplicate elements in .xml file using java?
I want to find the duplicate values in the xml file, if any of the duplicate element occurs then system should give an error/exceptionn
how to detect the duplicate elements in .xml file using java?
I want to find the duplicate values in the xml file, if any of the duplicate element occurs then system should give an error/exceptionn
Why don't use a schema definition (XSD) and occurences indicators ?
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="firstElement" type="xs:string"/>
<xs:element name="secondElement" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
The <all>
indicator specifies that the child elements can appear in any order, and that each child element must occur only once (When using the <all>
indicator you can set the <minOccurs>
indicator to 0 or 1 and the <maxOccurs>
indicator can only be set to 1
Source http://www.w3schools.com/schema/schema_complex_indicators.asp
See also What's the best way to validate an XML file against an XSD file? to use schema validation from Java