- google data api
- JAVAXP
code 如下
// import
import com.google.gdata.util.AuthenticationException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* This is a test template
*/
public class CalendarPureXml {
public static void main(String[] args) throws IOException, SAXException, AuthenticationException {
//---- set proxy if hava proxy-----------------------------------
Properties systemSettings = System.getProperties();
systemSettings.put("proxyPort","80");
systemSettings.put("proxyHost","proxy.quanta.corp");
//---------------------------------------------------------------
//----get XML by network , the example is my calendar------------
URL feedUrl = new URL("http://www.google.com/calendar/feeds/chiachunchuang%40gmail.com/private-6f9ef2889d8ccb2b63254d091d7cfc63/full");
URLConnection con = null;
try {
con = feedUrl.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = con.getInputStream();
//----------------------------------------------------------------
//----get XML-----------------------------------------------------
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = docBuilder.parse (is);
//normalize text redivsentation
doc.getDocumentElement ().normalize ();
//-----------------------------------------------------------------
//----print information--------------------------------------------
//print title
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
System.out.println("-------------------------------------------------");
//--EX 1 get child node---------------------------------------
NodeList UPEntry = (NodeList) doc.getDocumentElement().getChildNodes();
// 取得 item
Node ent=UPEntry.item(1);
System.out.println("child name 1 : " + ent.getNodeName());
System.out.println("child value 1 : " + ent.getTextContent());
//------------------------------------------------------
System.out.println("-------------------------------------------------");
//--EX 2 get node with title---------------------------------------
NodeList entTitle= doc.getElementsByTagName("title");
System.out.println("Total of Title : " + entTitle.getLength());
System.out.println("Title name 0 : " + entTitle.item(0).getNodeName());
System.out.println("Title value 0 : " + entTitle.item(0).getTextContent());
//------------------------------------------------------
System.out.println("-------------------------------------------------");
//--EX3 get entrys count which = entry------
NodeList listOfEntry = doc.getElementsByTagName("entry");
int totalEntrys = listOfEntry.getLength();
System.out.println("Total no of entry : " + totalEntrys);
//------------------------------------------------------------
// get entry value
// listOfEntry.item(int I) 第i個entry but entry 還有很多值
// 重複 getElementsByTagName( Name ) 取得想要的資訊
//------------------------------------------------------------
//-----------------------------------------------------------------
}
}
沒有留言:
張貼留言