@@ -2781,6 +2781,89 @@ public static String xmlToJson(String xml, XmlToJsonMode mode) {
2781
2781
return xmlToJson (xml , Json .JsonStringBuilder .Step .TWO_SPACES , mode );
2782
2782
}
2783
2783
2784
+ public static void fileXmlToJson (String xmlFileName , String jsonFileName , Json .JsonStringBuilder .Step identStep )
2785
+ throws IOException {
2786
+ final byte [] bytes = Files .readAllBytes (Paths .get (xmlFileName ));
2787
+ String xmlText = new String (removeBom (bytes ), detectEncoding (bytes ));
2788
+ Files .write (Paths .get (jsonFileName ), formatString (xmlToJson (xmlText , identStep ),
2789
+ System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ));
2790
+ }
2791
+
2792
+ public static void fileXmlToJson (String xmlFileName , String jsonFileName ) throws IOException {
2793
+ fileXmlToJson (xmlFileName , jsonFileName , Json .JsonStringBuilder .Step .TWO_SPACES );
2794
+ }
2795
+
2796
+ public static byte [] removeBom (byte [] bytes ) {
2797
+ if ((bytes .length >= 3 ) && (bytes [0 ] == -17 ) && (bytes [1 ] == -69 ) && (bytes [2 ] == -65 )) {
2798
+ return Arrays .copyOfRange (bytes , 3 , bytes .length );
2799
+ }
2800
+ if ((bytes .length >= 2 ) && (bytes [0 ] == -1 ) && (bytes [1 ] == -2 )) {
2801
+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
2802
+ }
2803
+ if ((bytes .length >= 2 ) && (bytes [0 ] == -2 ) && (bytes [1 ] == -1 )) {
2804
+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
2805
+ }
2806
+ return bytes ;
2807
+ }
2808
+
2809
+ public static String detectEncoding (byte [] buffer ) {
2810
+ if (buffer .length < 4 ) {
2811
+ return "UTF8" ;
2812
+ }
2813
+ String encoding = null ;
2814
+ int n = ((buffer [0 ] & 0xFF ) << 24 )
2815
+ | ((buffer [1 ] & 0xFF ) << 16 )
2816
+ | ((buffer [2 ] & 0xFF ) << 8 )
2817
+ | (buffer [3 ] & 0xFF );
2818
+ switch (n ) {
2819
+ case 0x0000FEFF :
2820
+ case 0x0000003C :
2821
+ encoding = "UTF_32BE" ;
2822
+ break ;
2823
+ case 0x003C003F :
2824
+ encoding = "UnicodeBigUnmarked" ;
2825
+ break ;
2826
+ case 0xFFFE0000 :
2827
+ encoding = "UTF_32LE" ;
2828
+ break ;
2829
+ case 0x3C000000 :
2830
+ encoding = "UTF_32LE" ;
2831
+ break ;
2832
+ // <?
2833
+ case 0x3C003F00 :
2834
+ encoding = "UnicodeLittleUnmarked" ;
2835
+ break ;
2836
+ // <?xm
2837
+ case 0x3C3F786D :
2838
+ encoding = "UTF8" ;
2839
+ break ;
2840
+ default :
2841
+ if ((n >>> 8 ) == 0xEFBBBF ) {
2842
+ encoding = "UTF8" ;
2843
+ break ;
2844
+ }
2845
+ if ((n >>> 24 ) == 0x3C ) {
2846
+ break ;
2847
+ }
2848
+ switch (n >>> 16 ) {
2849
+ case 0xFFFE :
2850
+ encoding = "UnicodeLittleUnmarked" ;
2851
+ break ;
2852
+ case 0xFEFF :
2853
+ encoding = "UnicodeBigUnmarked" ;
2854
+ break ;
2855
+ }
2856
+ }
2857
+ return encoding == null ? "UTF8" : encoding ;
2858
+ }
2859
+
2860
+ public static String formatString (String data , String lineSeparator ) {
2861
+ if ("\n " .equals (lineSeparator )) {
2862
+ return data ;
2863
+ }
2864
+ return data .replace ("\n " , lineSeparator );
2865
+ }
2866
+
2784
2867
public static String xmlOrJsonToJson (String xmlOrJson , Json .JsonStringBuilder .Step identStep ) {
2785
2868
TextType textType = getTextType (xmlOrJson );
2786
2869
final String result ;
0 commit comments