import java.io.*;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importorg.apache.http.HttpHost;importorg.apache.http.auth.AuthScope;importorg.apache.http.auth.NTCredentials;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.protocol.BasicHttpContext;importorg.apache.http.protocol.HttpContext;importjava.util.Iterator;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;public classTest {/***@paramargs*/
public static voidmain(String[] args) {//TODO Auto-generated method stub
DefaultHttpClient httpclient = newDefaultHttpClient();
NTCredentials creds= new NTCredentials("UsrName", "PassWord", "my", "contosouat");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
HttpHost target= new HttpHost("portal.contoso.uat", 80, "http");//Make sure the same context is used to execute logically related requests
HttpContext localContext = newBasicHttpContext();//Execute a cheap method first. This will trigger NTLM authentication
HttpGet httpget = new HttpGet("http://portal.contoso.uat/sites/cockpit/_vti_bin/listdata.svc/自定义列表");//httpget.setHeader("accept", "application/json");
HttpResponse response1 = null;try{
response1=httpclient.execute(target, httpget, localContext);//System.out.print(response1.toString());
} catch(ClientProtocolException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity1=response1.getEntity();try{
delFile("c:/自定义列表.xml");
saveFile("c:/自定义列表.xml",EntityUtils.toString(entity1),false);
SAXReader reader= newSAXReader();try{
Document document= reader.read(new File("c:/自定义列表.xml"));
Element rootElm=document.getRootElement();//枚举根节点下所有子节点
for (Iterator ie =rootElm.elementIterator(); ie.hasNext();)
{
Element element=(Element) ie.next();if(element.getName()=="entry")
{for (Iterator ic =element.elementIterator(); ic.hasNext();)
{
Element Celement=(Element) ic.next();if(Celement.getName()=="content")
{for (Iterator ip =Celement.elementIterator(); ip.hasNext();)
{
Element Pelement=(Element) ip.next();for (Iterator ix =Pelement.elementIterator(); ix.hasNext();)
{
Element Xelement=(Element) ix.next();if(Xelement.getName()=="人员名称" || Xelement.getName()=="应发工资")
{
System.out.println(Xelement.getName()+":"+Xelement.getData());
}
}//System.out.println(Pelement.getName());
}//System.out.println(Celement.getName());
}
}
}
}
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}public static void saveFile(String file, String data, boolean append) throwsIOException {
BufferedWriter bw= null;
OutputStreamWriter osw= null;
File f= newFile(file);
FileOutputStream fos= newFileOutputStream(f, append);try{//write UTF8 BOM mark if file is empty
if (f.length() < 1) {final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF};
fos.write(bom);
}
osw= new OutputStreamWriter(fos, "UTF-8");
bw= newBufferedWriter(osw);if (data != null) bw.write(data);
}catch(IOException ex) {throwex;
}finally{try { bw.close(); fos.close(); } catch(Exception ex) { }
}
}/*** 删除文件
*@paramfilePathAndName String 文件路径及名称 如c:/file.xml
*@paramfileContent String
*@returnboolean*/
public static voiddelFile(String filePathAndName) {try{
String filePath=filePathAndName;
filePath=filePath.toString();
java.io.File myDelFile= newjava.io.File(filePath);
myDelFile.delete();
}catch(Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
}