You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.3 KiB
Java
119 lines
3.3 KiB
Java
package com.thinkit.lewebconnect;
|
|
|
|
import java.io.IOException;
|
|
import java.io.StringReader;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.MalformedURLException;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
public class XMLfunctions {
|
|
|
|
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();
|
|
HttpGet get = new HttpGet(WebService.XML_DB_URL);
|
|
get.setHeader("charset", "utf-8");
|
|
|
|
HttpResponse httpResponse = httpClient.execute(get);
|
|
HttpEntity httpEntity = httpResponse.getEntity();
|
|
line = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
|
} 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 int numResults(Document doc){
|
|
NodeList results;
|
|
try {
|
|
results = doc.getElementsByTagName("user");
|
|
//Log.d("XMLFunctions", results.toString());
|
|
return results.getLength();
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static String getValue(Element item, String str) {
|
|
NodeList n = item.getElementsByTagName(str);
|
|
return XMLfunctions.getElementValue(n.item(0));
|
|
}
|
|
}
|