thuliobuarque / java2word

Automatically exported from code.google.com/p/java2word
0 stars 0 forks source link

XML template with special character support #76

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi!

I'm starting to use your libraries for a personal project. You have done a 
great work :)

When I use the function Utils.readFile(path) to load an XML template for my 
Word document, if I have special character like áíóú the XML have an 
enconding error or something like this. 

I checked the XML and if I remove the wrong characters I have a valid word 
document. Do you know how I can convert those wrong characters to the valid 
ones?

Original issue reported on code.google.com by alejandr...@gmail.com on 4 Jan 2012 at 12:01

GoogleCodeExporter commented 8 years ago
conversion causes issues. Make sure in myDocument.setencoding("your encoding") 
you set the encoding appropriated for your text. It should be the same of your 
OS. Remember: conversion causes issues. 

If it doesn't work, use the Hexa representation of those guys here áíóú. 
There have been some similar issues and that's how we solved last time I 
remember roughly. This should be the way to go if you have just a few special 
characters and you have to right code to replace them.  

cheers
Leonardo 

Original comment by leonardo...@gmail.com on 4 Jan 2012 at 12:55

GoogleCodeExporter commented 8 years ago
Hello Leonardo,

First of All, THANKS FOR YOUR ANSWER :)

It's seems an enconding error, but I'm not able to resolve it. Could you help 
me??? I'm trying to use an Word document as a XML Template. When I read it I 
can see problems with the encoding. Words like "Consejería" aren't read well.

Do you know how I should load the template?
I'm using this code to read the file:

                String xmlTemplate="";

            Writer writer2 = new StringWriter();
            char[] buffer = new char[5000];
            File f = new File("c:\\Users\\alejandro\\Desktop\\ReleaseNotesTemplate.xml");
            InputStreamReader rd = new InputStreamReader(new FileInputStream(f));

            Reader reader = new BufferedReader(rd);

            int n;
            try {
                while ((n = reader.read(buffer)) != -1) 
                {

                    writer2.write(buffer, 0, n);
                }
            } catch (IOException ex) {

            }
                xmlTemplate=writer2.toString();

And this code to save it:
 File fileObj = new File("c:\\Users\\alejandro\\Desktop\\SalidaTemplate.doc");

             PrintWriter writer = null;
                try {
                    writer = new PrintWriter(fileObj);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                writer.println(xmlTemplate);
                writer.close();

I have try to read it as  UTF8 and CP1252 but I still has encoding errors. 

Any ideas? I have Attached my template.

Thanks!!! 
Alejandro

Original comment by alejandr...@gmail.com on 12 Jan 2012 at 4:43

GoogleCodeExporter commented 8 years ago
I am working on it. let you know soon what is going on ok
hasta luego. Me de uma hora.  

Original comment by leonardo...@gmail.com on 16 Jan 2012 at 1:04

GoogleCodeExporter commented 8 years ago
this is a good issue to take a look: 
http://code.google.com/p/java2word/issues/detail?id=70

Cool Done!!!

At the line where you replace the placeholder, keep the value as you want to 
see in the final result:

   xmlTemplate = xmlTemplate.replace("prMainResearch", "Consejería");

Just before writing the actual salida template, replace all specials with the 
hexadecimal represantation:

  xmlTemplate = xmlTemplate.replace("í", "í");

Then you have the final document nicely formatted. 

THE CODE ABOVE IS HARCODED AND IT IS JUST A DEMOSTRATION. I will implement this 
in the API and it will be available in a beta version late today or tomorrow.

thanks for your feedback! can you please test the above code and see if that 
fixes at least "í"?

If so I go ahead with a proper solution ok?

gracias
Leonardo

Original comment by leonardo...@gmail.com on 16 Jan 2012 at 1:44

GoogleCodeExporter commented 8 years ago
Hi Leonardo,

I have found today a solution. It was an encoding error while opening and 
saving the files!

This is my code:

Open file

    String xmlTemplate="";

            Writer writer2 = new StringWriter();
            char[] buffer = new char[5000];
            File f = new File("c:\\Users\\alejandro\\Desktop\\ReleaseNotesTemplate.xml");
            InputStreamReader rd = new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8"));

            Reader reader = new BufferedReader(rd);

            int n;
            try {
                while ((n = reader.read(buffer)) != -1) 
                {

                    writer2.write(buffer, 0, n);
                }
            } catch (IOException ex) {

            }

Save file

  File fileObj = new File("c:\\Users\\alejandro\\Desktop\\SalidaTemplate.doc");
            OutputStream outputStream = new FileOutputStream(fileObj);

            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, Charset.forName("UTF-8")));

            writer.println(xmlTemplate);
            writer.close();

I have also try your code and it works!!

Thanks for your help and time :)

Original comment by alejandr...@gmail.com on 16 Jan 2012 at 10:03

GoogleCodeExporter commented 8 years ago
I will create a WIKI page about all encoding problems and post your and mime 
code.
thanks a for your feedback!

Original comment by leonardo...@gmail.com on 16 Jan 2012 at 10:36

GoogleCodeExporter commented 8 years ago
wiki for this kind of issue is: 
http://code.google.com/p/java2word/wiki/EncodingTipsandTricks

Original comment by leonardo...@gmail.com on 17 Jan 2012 at 12:35