외부 문자가 포함 된 xml을 구문 분석하려고하는데 (성공적으로 구문 분석하는 데 문제가 있습니다) 나는 어떤 오류도 내지 않지만, 편지는 이것으로 파싱된다; æ 메신저 대신에 à 얻는 대신, å 메신저 대신 à ¢ â를 받고 ø 메신저 대신 ¸¸ 얻으려고 나는 또한 문자를 발견 - 제대로 표시되지 않습니다. 내가 할 수있는 것을 깨달았다 .replaceAll은 3 글자이지만, 여기에 문제가 있다면 어딘가에서 실수를 저 지르거나 replaceAll의 경로를 내려 가지 않고 가능하지 않은지 확실하지 않다.DOM 및 특수 문자로 XML 구문 분석
코드 :
private Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(
xml.getBytes()));
// is.setCharacterStream(new StringReader(xml));
is.setEncoding("UTF-8");
Log.i(TAG, "Encoding: " + is.getEncoding());
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
// return DOM
return doc;
}
private String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
private final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}
는이보다 더 많은 코드를 볼 필요가 있다면 알려주세요.
감사의 말 - 감사합니다.
제안 해 주셔서 감사합니다. 다음 제안 사항을 시도했습니다. InputSource = new InputeSource (new ByteArrayInpuStream (xml.getBytes ("UTF-8")))); 및 InputSource = new InputSource (새 StringReader (xml)); 이러한 접근 방식 중 어떤 것도 차이가 없었습니다. is.setCharacterStream은이 문제를 해결하기위한 남은 시도이지만, 차이는없는 것 같습니다. – Line
@Line - 아마도 xml String이 처음 생성되었을 때 ('getDomElement'를 호출하기 전에) 인코딩이 엉망이었을 것입니다. 아마도 입력 문자열에 실제로 예상 한 문자가 있는지 확인하십시오. –
구문 분석하려고하는 xml의 예는 다음과 같습니다. HttpResponse loginResponse = loginClient.execute (loginPost); HttpEntity responseEnt = loginResponse.getEntity(); 문자열 결과 = EntityUtils.toString (responseEnt); // 이것은 파서에 주어진 문자열입니다. EnitityUtils.toString()이 문제를 일으킬 수 있습니까? 그러나 웹 사이트 API에서 전화로 구문 분석 된 다른 정보를 확인하기 위해 구문 분석 한 xml을 사용할 수 있습니다. – Line