gpt4 book ai didi

android - 如何通过utf-8编码从服务中解析XML特殊字符

转载 作者:行者123 更新时间:2023-11-30 03:02:25 24 4
gpt4 key购买 nike

我想从服务中解析“ALışVERIÅŸ”类型的内容。

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
myXMLHandler = new XMLHandler();
xr.setContentHandler(myXMLHandler);
URL _url = new URL(params[0]);
xr.parse(new InputSource(_url.openStream()));

最佳答案

我尝试使用 DOM Parser 而不是 XaxParser。检查下面的函数并在后台线程中调用它:

public String readXML(){
StringBuilder stringBuilder = new StringBuilder();
try {
URL _url = new URL("http://182.160.161.2/~siva/turkish/web_serv/category.php?order_by=desc&category_id=2");
File fXmlFile = new File(new InputSource(_url.openStream()).getByteStream().toString());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(_url.openStream()).getByteStream());

//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("document");

Node nResp = nList.item(0);
Element fstElmnt = (Element) nResp;
NodeList nameList1 = fstElmnt.getElementsByTagName("response");

Node mCat = nameList1.item(0);
Element catElmt = (Element) mCat;

NodeList catList = catElmt.getElementsByTagName("category");

System.out.println("----------------------------");

for (int temp = 0; temp < catList.getLength(); temp++) {

Node nNode = catList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

stringBuilder.append("Category ID : " + eElement.getElementsByTagName("category_name").item(0).getTextContent()+"\n");
stringBuilder.append("Category Name : " + eElement.getElementsByTagName("category_id").item(0).getTextContent());
System.out.println("Category ID : " + eElement.getElementsByTagName("category_name").item(0).getTextContent());
System.out.println("Category Name : " + eElement.getElementsByTagName("category_id").item(0).getTextContent());


}
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();

}

这是我的设备的屏幕截图,看看:你也可以看到那些土耳其语:)

enter image description here

关于android - 如何通过utf-8编码从服务中解析XML特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com