在R

时间:2016-06-02 07:44:25

标签: r xml dataframe

我尝试从网址上传XML并将其保存为CSV:

附上剧本:

query <-"https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2016-03-01&end-date=2016-03-30"
token <-"xxxx"

xmlfile <- xmlTreeParse(GET(url=query, add_headers(Authorization=token)))

但我收到了以下结构的结果。

    $doc
$file
[1] "<buffer>"

$version
[1] "1.0"

$children
$children$`cj-api`
<cj-api>
 <commissions total-matched="4">
  <commission>
   <action-status>closed</action-status>
   <action-type>advanced sale</action-type>
   <aid>10789406</aid>
   <commission-id>1965209327</commission-id>
   <country>US</country>
   <event-date>2016-03-02T04:22:04-0800</event-date>
   <locking-date>2016-05-10</locking-date>
   <order-id>1786924</order-id>
   <original>true</original>
   <original-action-id>1691086180</original-action-id>
   <posting-date>2016-03-02T05:03:45-0800</posting-date>
   <website-id>7991782</website-id>
   <action-tracker-id>337452</action-tracker-id>
   <action-tracker-name>JimmyJazz Sale</action-tracker-name>
   <cid>3010924</cid>
   <advertiser-name>Jimmy Jazz</advertiser-name>
   <commission-amount>0.16</commission-amount>
   <order-discount>0.00</order-discount>
   <sid/>
   <sale-amount>1.99</sale-amount>
  </commission>
  <commission>
   <action-status>locked</action-status>
   <action-type>advanced sale</action-type>
   <aid>12378040</aid>
   <commission-id>1969836131</commission-id>
   <country>IL</country>
   <event-date>2016-03-14T05:53:36-0700</event-date>
   <locking-date>2016-05-13</locking-date>
   <order-id>27307042</order-id>
   <original>true</original>
   <original-action-id>1695118411</original-action-id>
   <posting-date>2016-03-14T06:30:52-0700</posting-date>
   <website-id>7991782</website-id>
   <action-tracker-id>361197</action-tracker-id>
   <action-tracker-name>Sale</action-tracker-name>
   <cid>3848495</cid>
   <advertiser-name>boohoo.com</advertiser-name>
   <commission-amount>0.40</commission-amount>
   <order-discount>0.00</order-discount>
   <sid/>
   <sale-amount>2.88</sale-amount>
  </commission>
  <commission>
   <action-status>locked</action-status>
   <action-type>advanced sale</action-type>
   <aid>12378040</aid>
   <commission-id>1970220452</commission-id>
   <country>GB</country>
   <event-date>2016-03-15T03:15:11-0700</event-date>
   <locking-date>2016-05-14</locking-date>
   <order-id>27330813</order-id>
   <original>true</original>
   <original-action-id>1695483653</original-action-id>
   <posting-date>2016-03-15T04:01:28-0700</posting-date>
   <website-id>7991782</website-id>
   <action-tracker-id>361197</action-tracker-id>
   <action-tracker-name>Sale</action-tracker-name>
   <cid>3848495</cid>
   <advertiser-name>boohoo.com</advertiser-name>
   <commission-amount>0.60</commission-amount>
   <order-discount>0.00</order-discount>
   <sid>DnoAwoTTYtLs</sid>
   <sale-amount>4.31</sale-amount>
  </commission>
  <commission>
   <action-status>locked</action-status>
   <action-type>advanced sale</action-type>
   <aid>12378040</aid>
   <commission-id>1972164361</commission-id>
   <country>IL</country>
   <event-date>2016-03-20T06:15:41-0700</event-date>
   <locking-date>2016-05-19</locking-date>
   <order-id>27439097</order-id>
   <original>true</original>
   <original-action-id>1697317694</original-action-id>
   <posting-date>2016-03-20T07:00:46-0700</posting-date>
   <website-id>7991782</website-id>
   <action-tracker-id>361197</action-tracker-id>
   <action-tracker-name>Sale</action-tracker-name>
   <cid>3848495</cid>
   <advertiser-name>boohoo.com</advertiser-name>
   <commission-amount>1.01</commission-amount>
   <order-discount>0.00</order-discount>
   <sid>9rftdVKxGwud</sid>
   <sale-amount>7.24</sale-amount>
  </commission>
 </commissions>
</cj-api>


attr(,"class")
[1] "XMLDocumentContent"

$dtd
$external
NULL

$internal
NULL

attr(,"class")
[1] "DTDList"

attr(,"class")
[1] "XMLDocument"         "XMLAbstractDocument"

我试图做的其他选择是。

xmlfile2 <-
read.csv(text=rawToChar(
  GET(url=query, add_headers(Authorization=token))
  [["content"]]), header = TRUE, sep =',')

然后我以下列方式收到了数据:

X..xml.version.1.0.encoding.UTF.8..
1 <cj-api><commissions total-matched=4><commission><action-status>closed</action-status><action-type>advanced sale</action-type><aid>10789406</aid><commission-id>1965209327</commission-id><country>US</country><event-date>2016-03-02T04:22:04-0800</event-date><locking-date>2016-05-10</locking-date><order-id>1786924</order-id><original>true</original><original-action-id>1691086180</original-action-id><posting-date>2016-03-02T05:03:45-0800</posting-date><website-id>7991782</website-id><action-tracker-id>337452</action-tracker-id><action-tracker-name>JimmyJazz Sale  </action-tracker-name><cid>3010924</cid><advertiser-name>Jimmy Jazz</advertiser-name><commission-amount>0.16</commission-amount><order-discount>0.00</order-discount><sid></sid><sale-amount>1.99</sale-amount></commission><commission><action-status>locked</action-status><action-type>advanced sale</action-type><aid>12378040</aid><commission-id>1969836131</commission-id><country>IL</country><event-date>2016-03-14T05:53:36-0700</event-date><locking-date>2016-05-13</locking-date><order-id>27307042</order-id><original>true</original><original-action-id>1695118411</original-action-id><posting-date>2016-03-14T06:30:52-0700</posting-date><website-id>7991782</website-id><action-tracker-id>361197</action-tracker-id><action-tracker-name>Sale</action-tracker-name><cid>3848495</cid><advertiser-name>boohoo.com</advertiser-name><commission-amount>0.40</commission-amount><order-discount>0.00</order-discount><sid></sid><sale-amount>2.88</sale-amount></commission><commission><action-status>locked</action-status><action-type>advanced sale</action-type><aid>12378040</aid><commission-id>1970220452</commission-id><country>GB</country><event-date>2016-03-15T03:15:11-0700</event-date><locking-date>2016-05-14</locking-date><order-id>27330813</order-id><original>true</original><original-action-id>1695483653</original-action-id><posting-date>2016-03-15T04:01:28-0700</posting-date><website-id>7991782</website-id><action-tracker-id>361197</action-tracker-id><action-tracker-name>Sale</action-tracker-name><cid>3848495</cid><advertiser-name>boohoo.com</advertiser-name><commission-amount>0.60</commission-amount><order-discount>0.00</order-discount><sid>DnoAwoTTYtLs</sid><sale-amount>4.31</sale-amount></commission><commission><action-status>locked</action-status><action-type>advanced sale</action-type><aid>12378040</aid><commission-id>1972164361</commission-id><country>IL</country><event-date>2016-03-20T06:15:41-0700</event-date><locking-date>2016-05-19</locking-date><order-id>27439097</order-id><original>true</original><original-action-id>1697317694</original-action-id><posting-date>2016-03-20T07:00:46-0700</posting-date><website-id>7991782</website-id><action-tracker-id>361197</action-tracker-id><action-tracker-name>Sale</action-tracker-name><cid>3848495</cid><advertiser-name>boohoo.com</advertiser-name><commission-amount>1.01</commission-amount><order-discount>0.00</order-discount><sid>9rftdVKxGwud</sid><sale-amount>7.24</sale-amount></commission></commissions></cj-api>

运行以下

xmlfile <- xmlTreeParse(GET(url=query, add_headers(Authorization=token)), useInternalNodes = T)

xmlToDataFrame(xmlfile)

我收到了

    commission
1 closedadvanced sale107894061965209327US2016-03-02T04:22:04-08002016-05-101786924true16910861802016-03-02T05:03:45-08007991782337452JimmyJazz Sale  3010924Jimmy Jazz0.160.001.99
                                                                                                                                                                     NA
1 lockedadvanced sale123780401969836131IL2016-03-14T05:53:36-07002016-05-1327307042true16951184112016-03-14T06:30:52-07007991782361197Sale3848495boohoo.com0.400.002.88
                                                                                                                                                                                 NA
1 lockedadvanced sale123780401970220452GB2016-03-15T03:15:11-07002016-05-1427330813true16954836532016-03-15T04:01:28-07007991782361197Sale3848495boohoo.com0.600.00DnoAwoTTYtLs4.31
                                                                                                                                                                                 NA
1 lockedadvanced sale123780401972164361IL2016-03-20T06:15:41-07002016-05-1927439097true16973176942016-03-20T07:00:46-07007991782361197Sale3848495boohoo.com1.010.009rftdVKxGwud7.24

如何在数据框中保存此XML。

由于

0 个答案:

没有答案