1

I downloaded an xml file from web service. If I open file from file system is formed correctly, but when I run my code isn't formed correctly.

A part of xml file formed correctly, it opened from file system:

<?xml version="1.0" encoding="UTF-8"?><ns3:FatturaElettronica xmlns:ns3="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" versione="FPR12"> 

Here the same xml file managed by my code:

ÿþ<

I can't copy the code and I put an image of what I see on the eclipse console.

enter image description here

I tryed different ways to manage this file, but nothing worked.

This is the code that it manages files. I put all ways I tryed to solve the error.

private static String readFile(File file, Writer writerArg) throws FileNotFoundException, IOException,Exception
   {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        String typeEncoding = null;

        /*
         * First way
         * 
         * BufferedReader br = new BufferedReader(new FileReader(fileName));
                
           String nextLine = "";
           StringBuffer sb = new StringBuffer();
        
           while ((nextLine = br.readLine()) != null) 
          {   
           // System.out.println("Writing: " + nextLine);
           writerArg.write(nextLine);
           // sb.append(nextLine);
           sb.append(nextLine+"\n");
          }       // Convert the content into to a string
        String clobData = sb.toString().trim();
        */
        
        /*
         * Second way
         * 
         * fis = new FileInputStream(file);
           isr = new InputStreamReader(fis);
           typeEncoding = isr.getEncoding();
           Charset inputCharset = Charset.forName(typeEncoding);

           BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), inputCharset)); 
            
           String str;
           String nextLine = "";
           StringBuffer sb = new StringBuffer();

           while ((nextLine = in.readLine()) != null) {
              System.out.println(nextLine);
              writerArg.write(nextLine);
              // sb.append(nextLine);
              sb.append(nextLine+"\n");
           }
          String clobData = sb.toString().trim();
        // Return the data.
     return clobData;
     */
        
        
    /* Third way  */    
         String data = "";
            data = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
            System.out.println(data);
     return data;
   }

And when the below code receives the string I get the error: "String index out of range: -1"

schema=stringXml.substring(0,stringXml.indexOf("<FatturaElettronicaHeader")).trim();

The first way downloaded thousands of files and managed them. Only this file gives my this error. It's from yesterday that I'm looking for a way to solve the error.

Can anyone give my any idea?

Scripta14
  • 463
  • 2
  • 8
  • 22
  • XML says that it's UTF-8 encoded, but it's actually UTF-16. `ÿþ` is a representation of the UTF-16 byte-order mark. – Robby Cornelissen Jul 12 '22 at 07:30
  • thanks.. How can I modify my code? and which way of my code can I use it? – Scripta14 Jul 12 '22 at 07:32
  • Don't modify your code. Fix the file. Open it in an editor and save it as UTF-8. – Dawood ibn Kareem Jul 12 '22 at 07:37
  • I can't do it because the code takes files from file system. Is there a way to convert xml file in file system and working always with UTF-8 file? – Scripta14 Jul 12 '22 at 07:44
  • Well, all three variants of your code are reading the file using your system's default character set. And it looks like that's UTF-8. I guess you could write code that tries to guess what character set the file was actually saved in, but it will never be 100% reliable. You might find one of the answers to [this question](https://stackoverflow.com/q/499010) useful. – Dawood ibn Kareem Jul 12 '22 at 07:53
  • Thanks .. I solved it using a array string with a list of encodings and I test them until one matches the right encoding. – Scripta14 Jul 12 '22 at 10:31

0 Answers0