2017-11-12 3 views
2

안녕하세요 저는 R 및 XML 파일을 처음 사용합니다. 내가 좋아하는 몇 가지 옵션 시도중첩 된 XML을 데이터 프레임에서 R

<?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> 

:

가 나는 dataframe에이 XML SOAP 응답을 얻으려고 SO 및 기타 Google 검색에서 광범위한 연구 후

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) 

을, I 원하는 결과를 얻을 수 없습니다. 내가 얻을 수있는 것은 하나의 행과 다른 열의 모든 관측치가있는 데이터 프레임입니다.

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 

작업 테이블의 종류를 얻을 수있는 방법이 있나요 :

내가 좋아하는 READOUTS의 표를 얻으려고?

미리 감사드립니다.

답변

2

<PrepareDataByClientResponse> 태그에 기본 네임 스페이스 (예 : xmlns, 콜론으로 구분 된 접두어 없음)가 있기 때문에 모든 하위 노드가이 기본 네임 스페이스 아래를 따릅니다.

<READOUT> 태그를 구문 분석하려면 getNodeSet() 호출에 사용할 접두사를 선언하는 것이 좋습니다. 아래는 nm입니다. 당신이 가지고있는 것처럼 이러한 호출은 다음 dataframes에 쉽게 비교적 평탄한 XML을 마이그레이션 할 수있는 편리한 방법 xmlToDataFrame 내에서 사용할 수 있습니다

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 
+0

신난다, 그것은 마법처럼 일했다. 설명은 나와 같은 초보자에게도 매우 유용했습니다. 고맙습니다! –

+0

@JavierNevado - 듣고 기꺼이 도와 드리겠습니다. 해피 코딩! – Parfait

관련 문제