1

I'm using StAX to read XML file, but having problem with characters like žćčšđ. The code is almost same as in the SAX, but i had not that kind of problem with that.

this is part of xml document

<?xml version = "1.0" encoding="UTF-8" ?>      
<Autor>
        <Id>1</Id>
        <Meno>Jano Žiška</Meno>
        <Email>[email protected]</Email>
        <tel_cislo typ="mobil">0944564685</tel_cislo>  
        <plat>500</plat>
      </Autor>

java

        public static void main(String[] args) {
            try {
              XMLInputFactory f = XMLInputFactory.newInstance();
              XMLStreamReader r = f.createXMLStreamReader(new FileReader(SUBOR));
            }
....
          if (r.getLocalName().equals(ELEMENT_MENO) == true) {
            String v = r.getElementText();
             System.out.println("meno:\t\t\t " + v);
          }

how can i specify encoding in java? thanks

2 Answers 2

3

Unless you have a really good reason, you should always use binary streams with XML (InputStream/OutputStream), not character streams (Reader/Writer). using character streams risks corrupting the xml (as the OP's original code shows).

XMLStreamReader r = f.createXMLStreamReader(new FileInputStream( SUBOR ));
Sign up to request clarification or add additional context in comments.

1 Comment

Whar is it explained why this is the case?
0

Instead of using FileReader, do this:

XMLStreamReader r = f.createXMLStreamReader(
        new InputStreamReader(
                new FileInputStream( SUBOR ),
                Charset.forName( "UTF8" ) ) );

2 Comments

thanks, but i dont understand why is it different from SAX. I guess something in api..
Actually, you shouldn't be using a Reader at all, you should be using the InputStream directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.