0

我对 oData 服务很陌生,想从 lightswitch Odata 提要中查询(即挑选特定字段/记录)。有没有一种简单的方法可以在 Java 中做到这一点。我听说 oData 是“兼容的” - 如果是这样,任何人都可以让我开始。

我的提要如下所示:

<?xml version="1.0" encoding="utf-8"?><feed xml:base="http://my.ip.address/test5/testXPressDBData.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><id>http://pocketsize/test5/testXPressDBData.svc/dogs/</id><title type="text">dogs</title><updated>2012-07-24T00:29:39Z</updated><link rel="self" title="dogs" href="dogs" /><entry m:etag="W/&quot;3&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(1)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(1)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">1</d:id><d:name>hammy</d:name><d:age m:type="Edm.Int32">3</d:age><d:weight m:type="Edm.Single">25</d:weight><d:colour>brown</d:colour></m:properties></content></entry><entry m:etag="W/&quot;5&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(2)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(2)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">2</d:id><d:name>rufus</d:name><d:age m:type="Edm.Int32">5</d:age><d:weight m:type="Edm.Single">10</d:weight><d:colour>blonde</d:colour></m:properties></content></entry><entry m:etag="W/&quot;1&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(3)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(3)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">3</d:id><d:name>jenkins</d:name><d:age m:type="Edm.Int32">1</d:age><d:weight m:type="Edm.Single">5</d:weight><d:colour>spotted</d:colour></m:properties></content></entry><entry m:etag="W/&quot;12&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(4)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(4)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">4</d:id><d:name>billy</d:name><d:age m:type="Edm.Int32">12</d:age><d:weight m:type="Edm.Single">23</d:weight><d:colour>Purple</d:colour></m:properties></content></entry><entry m:etag="W/&quot;2&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(5)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(5)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">5</d:id><d:name>Bulgaria</d:name><d:age m:type="Edm.Int32">2</d:age><d:weight m:type="Edm.Single">3</d:weight><d:colour>brown</d:colour></m:properties></content></entry><entry m:etag="W/&quot;2&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(6)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(6)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">6</d:id><d:name>china</d:name><d:age m:type="Edm.Int32">2</d:age><d:weight m:type="Edm.Single">2</d:weight><d:colour>pink</d:colour></m:properties></content></entry></feed>
4

2 回答 2

1

看看 OData4J - 它为 OData 提供了很多基本支持,并帮助从提要中实现实体。

来自http://code.google.com/p/odata4j/source/browse/odata4j-fit/src/test/java/org/odata4j/examples/consumers/NetflixConsumerExample.java?name=0.6的示例代码:

package org.odata4j.examples.consumers;

import java.util.List;

import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;
import org.odata4j.core.OProperty;
import org.odata4j.examples.AbstractExample;
import org.odata4j.examples.ODataEndpoints;

public class NetflixConsumerExample extends AbstractExample {

  public static void main(String[] args) {
    NetflixConsumerExample example = new NetflixConsumerExample();
    example.run(args);
  }

  private void run(String[] args) {
    ODataConsumer c = this.rtFacde.create(ODataEndpoints.NETFLIX, null, null);

    // locate the netflix id for Morgan Spurlock
    int morganSpurlockId = c.getEntities("People").filter("substringof('Spurlock',Name)").execute().first().getProperty("Id", Integer.class).getValue();

    // lookup and print all titles he's acted in
    List<OEntity> titlesActedIn = c.getEntities("People").nav(morganSpurlockId, "TitlesActedIn").execute().toList();
    for (OEntity title : titlesActedIn) {
      for (OProperty<?> p : title.getProperties()) {
        report("%s: %s", p.getName(), p.getValue());
      }
      report("\n");
    }
    report("count: " + titlesActedIn.size());

  } 
} 
于 2012-07-24T21:49:23.977 回答
0

只需通过 SaxParser 运行它。然后喝点水。

http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

于 2012-07-24T01:16:30.757 回答