我正在編寫一個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;
}
}
沒有看到XML(及其編碼),解析它的代碼和代碼disp鋪設它,這將很難回答。 –
這很奇怪。它適用於我的應用程序。也許你應該發佈解析XML文檔並顯示它的代碼。 –
請檢查編碼 – nidhin