解析R中的XML响应

时间:2015-08-13 14:12:24

标签: xml r xml-parsing

我目前有一个来自SOAP API调用的XML响应,我将其解析为XMLInternalDocument罚款。我无法从解析的响应中提取特定节点。

以下是我尝试从XML响应中获取元素的方法。

library(XML)

responseparsed <- XML::xmlParse("response2.xml")
getNodeSet(responseparsed, "//Category")

我得到的回复是一个空列表(即没有我理解的内容)

# list()
# attr(,"class")
# [1] "XMLNodeSet"

如果可能的话,我最终想要将我的XML响应放到data.frame中,所以我真的很感激一些指示因为xmlToList我也无法使<header>工作。 }和&lt; body>都在XML数据包中。

如果您使用以下摘录并创建名为response2.xml的XML文件,那么您应该可以重现我的问题。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action>RetrieveResponse</wsa:Action>
    <wsa:MessageID>urn:uuid:mymessageid</wsa:MessageID>
    <wsa:RelatesTo>urn:uuid:relatestoid</wsa:RelatesTo>
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
    <wsse:Security><wsu:Timestamp wsu:Id="Timestamp-c74b9126-8c79-4624-abd1-de4021ce1096">
      <wsu:Created>2015-08-13T12:38:54Z</wsu:Created>
      <wsu:Expires>2015-08-13T12:43:54Z</wsu:Expires>
    </wsu:Timestamp></wsse:Security>
  </soap:Header>
  <soap:Body>
    <RetrieveResponseMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">
      <OverallStatus>OK</OverallStatus>
      <RequestID>myanonrequestid</RequestID>
      <Results xsi:type="List">
        <Client>
          <ID>6212693</ID>
        </Client>
        <PartnerKey xsi:nil="true" />
        <ID>537</ID>
        <ObjectID xsi:nil="true" />
        <ListName>All Subscribers</ListName>
        <Category>2151</Category>
        <Type>Private</Type>
      </Results>
      <Results xsi:type="List">
        <Client>
          <ID>6212693</ID>
        </Client>
        <PartnerKey xsi:nil="true" />
        <ID>1217</ID>
        <ObjectID xsi:nil="true" />
        <ListName>list A</ListName>
        <Category>3038</Category>
        <Type>Private</Type>
      </Results>
      <Results xsi:type="List">
        <Client>
          <ID>6212693</ID>
        </Client><PartnerKey xsi:nil="true" />
        <ID>1434</ID><ObjectID xsi:nil="true" />
        <ListName>List B</ListName>
        <Category>6362</Category>
        <Type>Private</Type>
      </Results>
      <Results xsi:type="List">
        <Client>
          <ID>6212693</ID>
        </Client>
        <PartnerKey xsi:nil="true" />
        <ID>1435</ID>
        <ObjectID xsi:nil="true" />
        <ListName> List C</ListName>
        <Category>6362</Category>
        <Type>Private</Type>
      </Results>
    </RetrieveResponseMsg>
  </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:4)

CategoryRetrieveResponseMsg祖先元素继承默认命名空间。要使用XPath引用命名空间中的元素,您需要映射前缀以指向命名空间uri,并在XPath中使用该前缀。我对r不是很熟悉,但我认为它会像:

getNodeSet(responseparsed, "//d:Category", c(d="http://exacttarget.com/wsdl/partnerAPI"))