3

Hello I'm new to R and XML files.

I'm trying to get this XML SOAP response into a dataframe:

<?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">
  <soap:Body>
    <PrepareDataByClientResponse xmlns="urn:HM-schema">
      <PrepareDataByClientResult>
        <READOUT>
          <SerialNumber>1728527</SerialNumber>
          <Date>1510505992000</Date>
          <Type>1</Type>
          <Value>78.2</Value>
          <Status>OK</Status>
        </READOUT>
        <READOUT>
          <SerialNumber>1728527</SerialNumber>
          <Date>1510509592000</Date>
          <Type>1</Type>
          <Value>76.87</Value>
          <Status>OK</Status>
        </READOUT>
        <READOUT>
          <SerialNumber>1728527</SerialNumber>
          <Date>1510513192000</Date>
          <Type>1</Type>
          <Value>75.61</Value>
          <Status>OK</Status>
        </READOUT>
        <READOUT>
          <SerialNumber>e2ddeed13b4cc4d132f8c6a67d67eed3</SerialNumber>
          <Date>4531528776000</Date>
          <Type>3</Type>
          <Value>230.68</Value>
          <Status>OK</Status>
        </READOUT>
      </PrepareDataByClientResult>
        </PrepareDataByClientResponse>
      </soap:Body>
    </soap:Envelope>

I have tried several options like:

xmlout <- do.call(rbind, xpathApply(xmldoc,'//soap:Envelope/soap:Body/PrepareDataByClientResponse', xmlToDataFrame))
xmlout <- as.data.frame(t(xpathSApply(xmldoc,"//readout",function(x) xmlSApply(x,xmlValue))))
xmlout <- as.data.frame(t(xmlSApply(xmldoc["/PrepareDataByClientResponse/PrepareDataByClientResult/READOUT"],xmlAttrs)),stringsAsFactors=FALSE)
xmlout <- ldply(xmlToList(xmldoc), data.frame)

After extensive research in SO and other google searches, I have been unable to produce the desired results. All I can get is a data frame with a single row and all the observations in a different column each.

I'm trying to get a table of READOUTS like:

    SerialNumber    Date            Type    Value    Status
1   1728527         1510505992000   1       78.2     OK
2   1728527         1510509592000   1       76.87    OK
3   1728527         1510513192000   1       75.61    OK

Is there any way to get this sort of table to work?

Thanks in advance.

1 Answer 1

3

Because you have a default namespace at the <PrepareDataByClientResponse> tag (i.e., xmlns without a colon separated prefix), all its children follow under this default namespace.

To parse the <READOUT> tags, consider declaring a prefix to use in a getNodeSet() call. Below uses nm. Such a call can then be used inside the convenience method xmlToDataFrame which can easily migrate relatively flat XML like you have into dataframes:

library(XML)

doc <- xmlParse('/path/to/SOAP/Response.xml')

df <- xmlToDataFrame(doc, nodes=getNodeSet(doc, "//nm:READOUT",
                                           namespaces=c(nm="urn:HM-schema")))

df
#                       SerialNumber          Date Type  Value Status
# 1                          1728527 1510505992000    1   78.2     OK
# 2                          1728527 1510509592000    1  76.87     OK
# 3                          1728527 1510513192000    1  75.61     OK
# 4 e2ddeed13b4cc4d132f8c6a67d67eed3 4531528776000    3 230.68     OK
2
  • Awesome, it did work like a charm. The explanation was also very useful for a newbie like me. Thank you! Commented Nov 13, 2017 at 8:54
  • @JavierNevado - Great to hear and glad to help. Happy coding!
    – Parfait
    Commented Nov 13, 2017 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.