Java: XML canonicalization

前端 未结 2 1437
情歌与酒
情歌与酒 2020-12-14 03:08

What\'s the easiest way to make a canonical form of a XML file in Java? Do you have some done code for that? I\'ve found several links on the net, like this, this, and this,

相关标签:
2条回答
  • 2020-12-14 03:34

    The Canonicalizer class at Apache XML Security project.

    Initialize the library.

    org.apache.xml.security.Init.init(); 
    

    Convert your XML.

    Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    byte canonXmlBytes[] = canon.canonicalize(yourXmlBytes);
    String canonXmlString = new String(canonXmlBytes);
    
    0 讨论(0)
  • 2020-12-14 03:36

    Another option is nu.xom.canonical.Canonicalizer if you're using XOM, or if you don't otherwise have a need for Apache XML Security.

    0 讨论(0)
提交回复
热议问题