浏览 2173 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-01
直接贴代码了, 具体的和ibm 门户完全关联,使用atom 。主要是获得元素的属性值的时候遇到了一些问题,本来使用xpath 的基本形式(/feed/entry/tile/@type)没有问题的,但是/feed/entry/id的属性使用了命名空间,按照上面的模式都不成功,最后只好使用qname形式的参数获得属性值,这基本花了半天的时间才解决。
写道
package test;
import java.io.InputStream; import java.net.URISyntaxException; import javax.xml.namespace.QName; import org.apache.abdera.Abdera; import org.apache.abdera.model.Document; import org.apache.abdera.model.Element; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.parser.Parser; import org.apache.abdera.protocol.client.AbderaClient; import org.apache.abdera.protocol.client.ClientResponse; import org.apache.abdera.xpath.XPath; import org.apache.commons.httpclient.UsernamePasswordCredentials; public class NewTesta { /** * @param args */ public static void main(String[] args) { NewTesta newTest = new NewTesta(); try { newTest.getPageInfo(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void getPageInfo() throws Exception { Abdera abdera = new Abdera(); AbderaClient client = new AbderaClient(abdera); client.usePreemptiveAuthentication(true); try { client.addCredentials("http://127.0.0.1:10040/wps/myportal", null, null, new UsernamePasswordCredentials("admin", "password")); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 替换为具体的唯一标识 oid:6_CGAH47L00O2V002N5SQ0US3867 ClientResponse response1 = client .get("http://127.0.0.1:10040/wps/mycontenthandler?uri=cm:oid:6_CGAH47L00O2V002N5SQ0US3867"); Parser parser = Abdera.getNewParser(); InputStream in = response1.getInputStream(); Document<Feed> doc = parser.parse(in); Feed feed = doc.getRoot(); Entry entry = feed.getEntries().get(0); System.out.println(entry.getTitle()); System.out.println(entry.getTitleType()); Element theId = entry.getElements().get(1); XPath xpath = abdera.getXPath(); System.out.println(feed.toString()); org.apache.abdera.parser.stax.FOMIRI o = (org.apache.abdera.parser.stax.FOMIRI) xpath .selectSingleNode("/a:feed/a:entry/a:id", feed); QName newQname = new QName( "http://www.ibm.com/xmlns/prod/websphere/portal/v6.0.1/portal-model", "uniquename", "portal"); System.out.println(o.getAttributeValue(newQname)); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |