2011-08-03 145 views
0

我正在編寫一個Java程序,它解析XML。我的問題是,像äöü這樣的特殊符號不會顯示在我的應用程序中。但其餘的文本呢。例如:XML解析Java Java Java

Oliver Krähnbühl => Oliver Krhnbhl 

我不能通過編碼XML來做一些事情。因爲它通過HTTP-Request加載。

這裏是解析器的代碼:

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 properties 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; 
    } 
} 
+3

沒有看到XML(及其編碼),解析它的代碼和代碼disp鋪設它,這將很難回答。 –

+1

這很奇怪。它適用於我的應用程序。也許你應該發佈解析XML文檔並顯示它的代碼。 –

+1

請檢查編碼 – nidhin

回答

3

你嘗試使用輸入流讀取器?

類似:

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

,並使用StreamSourceInputSource創建XML,是這樣的:

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

還承擔你的輸出方法看,例如試試這個:

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

服務器應該發送指定編碼的內容類型頭文件,或者解析器應該通過查看前幾個字符和xml序言碼來確定它。在這種情況下,硬編碼UTF-8是錯誤的。 –

+2

@Jorn同意,但總的來說答案是有幫助的 - 所以我是投票。 – Mikaveli