3

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

awatto
  • 231
  • 5
  • 16

3 Answers3

6

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

Introduction to the Java Digital XML Signature APIs

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
2

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.

Community
  • 1
  • 1
Bert F
  • 85,407
  • 12
  • 106
  • 123
1

check the standard W3C standard 'c14n'. + ignor whitespace. It will even handle the namespaces. For sure in you libarry you have an implemntation