0

I am parsing this XML file:

<?xml version="1.0" encoding="UTF-8"?>

<tests>
    <test category="Русский"/>
    <test category="ελληνικά"/>
    <test category="中文"/>
    <test category="English"/>
</tests>

Main class is:

import java.io.File;
import java.io.FileInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class TestUnicode {
    public static void main(String[] args) throws Exception {
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression lolwhy = xpath.compile("//test");
        final InputSource inputSource =
                new InputSource(
                new FileInputStream(
                new File("sample.xml")));
        NodeList parent = (NodeList) lolwhy.evaluate(
                inputSource,
                XPathConstants.NODESET);
        System.out.println(parent.getLength());
        for (int i = 0; i < parent.getLength(); i++) {
            System.out.println(parent.item(i).getAttributes().
                    getNamedItem("category").getNodeValue());
        }
    }
}

And the output is:

4
???????
????????
??
English

What am I doing wrong here?

EDIT: ok, this issue was related to hebrew appears as question marks in netbeans and the solution is this: Setting the default Java character encoding?

Community
  • 1
  • 1
zbstof
  • 1,052
  • 3
  • 14
  • 27

2 Answers2

0

Could be that the parsing is ok, but the output is wrong.

If you you used a font that doesn't contain those characters, or if you output the values to HTML, but specify a wrong encoding, this can be the result.

The font-issue being the more likely one.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Yes, seems to be some kind of console output problem in Netbeans, but funny thing is - it seem to print garbage no matter what font i use. – zbstof Jun 05 '11 at 14:59
0

System.out.println is the culprit. See if this helps

http://hints.macworld.com/article.php?story=20050208053951714

DmitryK
  • 5,542
  • 1
  • 22
  • 32
  • Thanks, but PrintStream workaround only prints: 4 РуÑ�Ñ�кий ελληνικά 中文 English – zbstof Jun 05 '11 at 14:57