0

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

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 1
    How do *you* define a duplicate? Must each element appear only once? Are the same elements allowed so long as no attributes match? Or some subset of attributes? Please add some samples to your question, e.g. some XML that must be accepted, and then one or two examples of XML that must be rejected. – Damien_The_Unbeliever Oct 13 '14 at 14:00

1 Answers1

0

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

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Alfonso Oct 13 '14 at 15:10
  • well one link is a SO answer and will so never never be broken, the other one is a reference documentation. For this last should i have copy and pasted the interest part here ? – Gab Oct 13 '14 at 15:36
  • An answer should be self contained. So yes, please copy the relevant parts into this answer and use the link as reference/source. – Alfonso Oct 13 '14 at 15:47
  • done, but i must say that i find it useless for links to reference documentations – Gab Oct 13 '14 at 16:09