PHP 요청 URI에서 오는 XML을 구문 분석하려고합니다 : http://caracasfutbolclub.com/service/news.php. String xml이 구문 분석 된 후 로그를 작성하면 응답이 완료되고 '<'을 '& lt;'으로 변환하는 것 외에는 모두 잘 보입니다. 모든 HTML 태그 (UTF-8 이슈 또는 다른 코드화 일 수 있음)로 계속 진행됩니다. 실제 거래는 노드에서 요소를 요청할 때 '제목'XML 태그가 검색되지만 문제는 인코딩 된 HTML 대신 '<'만 표시하는 'introtext'태그입니다. 태그 내부 :안드로이드에서 인코딩 된 HTML로 XML을 구문 분석하는 방법
참고 : "map.put ("introtext ", XMLfunctions.getValue (e,"introtext "));"를 로그하면 표시 될뿐만 아니라 전체 문자열 <입니다.
MainActivity :
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML(); // method that is parsing the whole XML as a String.
Document doc = XMLfunctions.XMLfromString(xml);
Log.d("XML" , xml);
NodeList nodes = doc.getElementsByTagName("New");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("title", XMLfunctions.getValue(e, "title"));
map.put("introtext", XMLfunctions.getValue(e, "introtext"));
map.put("created", "Publicado: " + XMLfunctions.getValue(e, "created"));
mylist.add(map);
}
XMLFuntions : 내가 사용
코드는 다음과
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue(Node elem) {
Node kid;
if(elem != null){
if (elem.hasChildNodes()){
for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()){
if(kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}
public static String getXML(){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://caracasfutbolclub.com/service/news.php");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return XMLfunctions.getElementValue(n.item(0));
}
}
내가 추가 할 경우이 일할 수는 ' AdolfoFermo
당신은 xml이란 무엇입니까? xml 파서가 HTML 문자열을 건너 뛰고 '<'로 혼동하지 않기를 원했습니다. CDATA가이를 수행합니다. 여전히 문제가있는 경우 xml을 게시하십시오. – Orlymee
코드에서 'xml'이라는 코드의 변수 xml을 의미합니다. 태그를 검색 한 다음 이 발견 될 때마다 CDATA 행을 추가해야합니까? 또는 유일한 방법은 webservice를 수정하여 해당 라인을 추가하는 것입니다 ... ... 내가 처음에 게시 한 PHP 링크의 XML을 확인할 수 있습니다 –
AdolfoFermo