开发者

XML parsing äöü Java

I'm programming a program in Java, which is parsing XML. My Problem is, that the special signs like ä, ö, ü aren't show in my application. But the rest of the text yet. Example:

Oliver Krähnbühl  => Oliver Krhnbhl

I can't do something by the coding of the XML. Because its loaded by HTTP-Request.

Here is the code of the parser:

public Boolean parse(String url) {
    try {
        InputStream inStream = (InputStream) new URL(url).getContent();


        // TODO: after we must do a cache of this XML!!!!
        this.factory = DocumentBuilderFactory.newInstance();
        this.builder = this.factory.newDocumentBuilder();
        this.builder.isValidating();
        Document doc = this.builder.parse(inStream, null);

        doc.getDocumentElement().normalize();

        //Get all categories
        NodeList categoryList = doc.getElementsByTagName("Category");

        //Loop each category
        for (int i = 0; i < categoryList.getLength(); i++) {
            //Get categoryname
            final NamedNodeMap attr     = categoryList.item(i).getAttributes();
            final String categoryName   = getNodeValue(attr, "name");

            //Add a category separator
            productSeparator s = new productSeparator(categoryName);
            this.list.add(s);

            //Get current Category as element
            Element category        = (Element)categoryList.item(i);

            //Get all Products from current category
            NodeList productList    = category.getElementsByTagName("Product");

            //Loop each element from each category
            for(int x = 0; x < productList.getLength(); x++)
            {
                //Get current Product as element
                Element product     = (Element)productList.item(x);

                //Set prope开发者_如何学Gorties to variable

                String productName          = (((Element)product.getElementsByTagName("Name").item(0)).getChildNodes()).item(0).getNodeValue();
                String productDescription   = (((Element)product.getElementsByTagName("Description").item(0)).getChildNodes()).item(0).getNodeValue();
                String productPrice         = (((Element)product.getElementsByTagName("Price").item(0)).getChildNodes()).item(0).getNodeValue();
                String productImageUri      = (((Element)product.getElementsByTagName("ImageUri").item(0)).getChildNodes()).item(0).getNodeValue();

                // Construct Country object
                product p = new product(productName, productDescription, new Float(productPrice), productImageUri);

                // Add to list
                this.list.add(p);
            }
        }
        return true;
    } 
    catch (Exception er) {
        Log.e("Exception", er.toString());
        return false;
    }
}


did you try to use a input stream reader?

something like:

Reader reader 
   = new InputStreamReader((InputStream) new URL(url).getContent(), "utf-8");

and use a StreamSource or InputSource to create the XML, something like:

InputSource src = new InputSource(reader);
Document doc = this.builder.parse(src);

also take a look on your output method, for example try this:

try
{
  // output to the console
  Writer w =
     new BufferedWriter
        (new OutputStreamWriter(System.out, "utf-8"));
  w.write("looks good: äöü\n"); // looks good
  w.flush();

  w = new BufferedWriter
         (new OutputStreamWriter(System.out, "Cp850"));
  w.write("looks bad: äöü"); // looks bad
  w.flush();
  w.close();
}
catch (Exception e)
{
  e.printStackTrace();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜