tags:

views:

66

answers:

2

Hi,

I need to create a checksum for an XML file in Java. The basic requirements are:

  1. The order of elements matters;
  2. The name-value pair of attributes is important, but the order of attributes is NOT;
  3. Ignore all white spaces and comments

Anyone can provide any hint or sample code?

Thanks, Mark

+4  A: 

You can make use of the Java Digital XML Signature APIs:

Introduction to the Java Digital XML Signature APIs

0xA3
Thanks for the info. If I have to stick with Java SE 5, is there any other way to do so?
awatto
+1  A: 

Method 1: Use XSLT to normalize the document.

Essentially you would use XSLT to normalize XML documents so that equivalent documents distill down into the same document. The transformation would:

  1. Maintain element order
  2. Order the attributes of each element (e.g. alphabetize based on the attribute name)
  3. Strip the whitespace and comments

You would then checksum the normalized version of the document.

Some useful references:

Method 2. Use a DOM parser

  1. Use a DOM parser to produce a DOM tree
  2. Normalize the DOM tree according to your rules
  3. Traverse the tree and feed the XML items to a checksum calculator

Method 3. Use a SAX or StAX parser

If you don't like the intermediate step of producing a normalized document or DOM tree, you could use SAX or StAX to parse the XML to maintain/order/strip like above on the fly and feed each element/content/attribute/value/etc to a checksum calculator.

Bert F
Thanks a lot for taking time and providing such detailed answers. Really appreciate it.
awatto