0

我正在尝试使用 scala.xml.XML 解析来自 AWIS(来自亚马逊的 alexa rest apis)api 的 xml 响应,但没有成功。

XML 响应:

<?xml version="1.0"?>
<aws:TrafficHistoryResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>aef82596-5d7b-c72a-b97c-f31de4aedcf5</aws:RequestId></aws:OperationRequest><aws:TrafficHistoryResult><aws:Alexa>

  <aws:TrafficHistory>
    <aws:Range>7</aws:Range>
    <aws:Site>yahoo.com</aws:Site>
    <aws:Start>2015-10-30</aws:Start>
    <aws:HistoricalData>
      <aws:Data>
        <aws:Date>2015-10-30</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>10800</aws:PerMillion>
          <aws:PerUser>7.33</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>130500</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-10-31</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>10790</aws:PerMillion>
          <aws:PerUser>6.85</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>129100</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-11-01</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>10960</aws:PerMillion>
          <aws:PerUser>6.84</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>132500</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-11-02</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>11180</aws:PerMillion>
          <aws:PerUser>7.57</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>134800</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-11-03</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>11290</aws:PerMillion>
          <aws:PerUser>7.70</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>133900</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-11-04</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>11310</aws:PerMillion>
          <aws:PerUser>7.76</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>133400</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
      <aws:Data>
        <aws:Date>2015-11-05</aws:Date>
        <aws:PageViews>
          <aws:PerMillion>11430</aws:PerMillion>
          <aws:PerUser>7.74</aws:PerUser>
        </aws:PageViews>
        <aws:Rank>5</aws:Rank>
        <aws:Reach>
          <aws:PerMillion>133300</aws:PerMillion>
        </aws:Reach>
      </aws:Data>
    </aws:HistoricalData>
  </aws:TrafficHistory>
</aws:Alexa></aws:TrafficHistoryResult><aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:StatusCode>Success</aws:StatusCode></aws:ResponseStatus></aws:Response></aws:TrafficHistoryResponse>

处理代码:

val xml = XML.loadString(response.body)
val response = xml \ "aws:Response"
val data = xml \\ "aws:Data"

响应和数据都返回空节点序列。我想这可能与命名空间有关。

任何有关如何解决此问题的指示将不胜感激。TIA

4

1 回答 1

1

尝试请求没有指定命名空间的元素:

val response = xml \ "Response"
val data = xml \\ "Data"
于 2015-11-07T15:58:56.077 回答