Solved

UTF8.txt file to UTF8 xml

Posted on 2006-07-05
9
270 Views
Last Modified: 2008-02-01
Hi.
 I have a UTF8 file that I need to read and convert to xml (allso utf8).
The UTF file is in CZECH and I'm on an US system.
I have tree questions:
I'm reading the file like this:
            input = new BufferedReader( new FileReader(aFile) );
            String line = null;
            int i = 0;
            while (( line = input.readLine()) != null && i < 100){
                i ++;
                line.trim();
                System.out.println("l:" + line);

1. When I print it out, I get lines like: Oznámení zadávacího řízenÃ. And that does not look correct. (I don't know Czech, but I'd ecpect som Czech letters.) ­ Any Idea what I do wrong?
2. I need to read the file line for line and I neet to remove "\t" "\n" and "  " from the start of the line. Is there any way I can print these "hidden" chars so I can see what the original file is using?
3. I have some text like this: "
  TI: UK-Cardiff: KOBO CESIE EEIG  
  PD: 20060620
  ND: 121873-2006"
And I need to convert it to "<ti><country>UK</country><city>Cardiff</city><name>KOBO CESIE EEIG</name></ti>
<pd>20060620</pd>
<nd>121873-2006</nd>
Any ideas how I can split the text like this? Just to make things worse, sometimes the TI: text has several lines separatet with "\n" and serveral " " (banks or space).

0
Comment
Question by:kristian_gr
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to use a font that support Czech charcters

>>line.trim();

should be

line =
line.trim();
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
try:

            input = new BufferedReader( new InputStreamReader(new FileInputStream(aFile), "UTF8") );

you'll also need to have a font installed that support czech to display it
0
 
LVL 14

Expert Comment

by:StillUnAware
Comment Utility
System.out.println(...) uses a console or a command line, so don't expect to see there the foreign charaters, You could see them only in an awt or swing component like JTextArea and only in the font which has the glyphs defined for the language You are using. I would suggest You to convert the text file to xml and only then check whether the characters are recognizable.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Just to make things worse,

If it's not too big, you'd be better off reading it all into one String
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:kristian_gr
Comment Utility
ok, this seams to work:  
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("c:/test.txt")), "UTF8"));
            BufferedReader input = new BufferedReader( new InputStreamReader(new FileInputStream(aFile), "UTF8") );
            String line = null;
            int i = 0;
            while (( line = input.readLine()) != null && i < 400){
                i ++;
//                line = line.trim();
                w.write(line);
                w.newLine();
            }
            w.cl

If I open my test.txt in word it seams to have the right cahracters.
Using trim is not a good idea. The file is formated in a way that using 4 blanks/space in the start of a line, it indicates that it realy belongs to the line above.
I therfore thik I need to do if(! String line starts with 4 blanks){w.newline();}
But how can I test if a line starts with 4 blanks?  
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
>>But how can I test if a line starts with 4 blanks?  

if (line.startsWith("    "))
0
 

Author Comment

by:kristian_gr
Comment Utility
blush!
Some days even the easiest things it hard. This is one of those.
tnx CEHJ.

And sinse I know you are good at regex'es, you probably have an idea about this:
Some of my lines starts with XX: as in two Uppercase chars, and a ":". If that occures I'd like to split the String at the first ":" into String[0] and String[1]. But the text in String[1] might allso contain serveral ":".
eks: String test = "PT: testString : has some more text";
String[0] result: PT
String[1] result: "testString : has some more text";
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
if line.matches("^[A-Z]{2}:.+")) {
    String[]  tokens  = line.split(":", 2);
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
how does that answer your original question?
the important thing was to specify the appropriate encoding.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now