• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing two node elements of a XML file  RSS feed

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Here's a sample XML -

<Data>
<Node1>
<Elem1>123</Elem1>
<Elem2>abc</Elem2>
<Elem3>xyz</Elem3>
</Node1>
<Node1>
<Elem1>999</Elem1>
<Elem2>uvw</Elem2>
<Elem3>000</Elem3>
</Node1>
<Node2>
<Elem1>123</Elem1>
<Elem2>abc</Elem2>
<Elem3>xyz</Elem3>
</Node2>
<Node2>
<Elem1>123</Elem1>
<Elem2>uvw</Elem2>
<Elem3>000</Elem3>
</Node2>
</Data>

In the above xml we have multiple occurences of Node1 and Node2. Node1 and Node2 are tied with each other by the value of Elem1. This Elem1 value should be unique through out the XML. So, in other words Node1 can be related to only one Node2 by the Elem1 value. But in the example there are two Node2 with the same Elem1 value - '123'. I like to raise an exception if this occurs.
What would be the best way to solve this problem?

Thanking you all.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tariq Ahsan:
Hello All,

What would be the best way to solve this problem?



That depends on if you have a schema. If you have a schema, and it defines the limits you discuss, you can simply validate against the schema. To me that would be the best and easiest way. That way if the schema changes, no code changes are needed on your part.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an article on XML Schema Validation: http://www.oracle.com/technology/pub/articles/vohra_xmlschema.html

For more, Google Java Validating XML schema
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark I would look into the link you've mentioned.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!