R xml2删除空节点

时间:2019-09-11 17:28:54

标签: r xml2

我正在尝试从返回带有NULL NODES的XML文件的API中获取数据,我需要那些知道丢失数据的地方。请参见下面的XML,节点CHUVA07至CHUVA11。

<?xml version="1.0" encoding="UTF-8"?>
<DataTable xmlns="http://MRCS/">
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">...</xs:schema>
   <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <DocumentElement xmlns="">
         <SerieHistorica diffgr:id="SerieHistorica1" msdata:rowOrder="0">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica2" msdata:rowOrder="1">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica3" msdata:rowOrder="2">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica4" msdata:rowOrder="3">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica5" msdata:rowOrder="4">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica6" msdata:rowOrder="5">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica7" msdata:rowOrder="6">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica8" msdata:rowOrder="7">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica9" msdata:rowOrder="8">...</SerieHistorica>
         <SerieHistorica diffgr:id="SerieHistorica10" msdata:rowOrder="9">
            <EstacaoCodigo>1943022</EstacaoCodigo>
            <NivelConsistencia>1</NivelConsistencia>
            <DataHora>2015-03-01 00:00:00</DataHora>
            <TipoMedicaoChuvas>1</TipoMedicaoChuvas>
            <Maxima>44.8</Maxima>
            <Total>134.9</Total>
            <DiaMaxima>24</DiaMaxima>
            <NumDiasDeChuva />
            <MaximaStatus>1</MaximaStatus>
            <TotalStatus>1</TotalStatus>
            <NumDiasDeChuvaStatus>0</NumDiasDeChuvaStatus>
            <TotalAnual />
            <TotalAnualStatus>0</TotalAnualStatus>
            <Chuva01>2.6</Chuva01>
            <Chuva02>0.5</Chuva02>
            <Chuva03>0</Chuva03>
            <Chuva04>0</Chuva04>
            <Chuva05>0</Chuva05>
            <Chuva06>18.5</Chuva06>
            <Chuva07 />
            <Chuva08 />
            <Chuva09 />
            <Chuva10 />
            <Chuva11 />
            <Chuva12>29.7</Chuva12>
            <Chuva13>0</Chuva13>
            <Chuva14>1.8</Chuva14>
            <Chuva15>0</Chuva15>
            <Chuva16>0</Chuva16>
            <Chuva17>0</Chuva17>
            <Chuva18>0</Chuva18>
            <Chuva19>0</Chuva19>
            <Chuva20>0</Chuva20>
            <Chuva21>14.8</Chuva21>
            <Chuva22>1</Chuva22>
            <Chuva23>5.7</Chuva23>
            <Chuva24>44.8</Chuva24>
            <Chuva25>1.1</Chuva25>
            <Chuva26>0</Chuva26>
            <Chuva27>0</Chuva27>
            <Chuva28>0</Chuva28>
            <Chuva29>0</Chuva29>
            <Chuva30>0</Chuva30>
            <Chuva31>14.4</Chuva31>
            <Chuva01Status>1</Chuva01Status>
            <Chuva02Status>1</Chuva02Status>
            <Chuva03Status>1</Chuva03Status>
            <Chuva04Status>1</Chuva04Status>
            <Chuva05Status>1</Chuva05Status>
            <Chuva06Status>1</Chuva06Status>
            <Chuva07Status>0</Chuva07Status>
            <Chuva08Status>0</Chuva08Status>
            <Chuva09Status>0</Chuva09Status>
            <Chuva10Status>0</Chuva10Status>
            <Chuva11Status>0</Chuva11Status>
            <Chuva12Status>4</Chuva12Status>
            <Chuva13Status>1</Chuva13Status>
            <Chuva14Status>1</Chuva14Status>
            <Chuva15Status>1</Chuva15Status>
            <Chuva16Status>1</Chuva16Status>
            <Chuva17Status>1</Chuva17Status>
            <Chuva18Status>1</Chuva18Status>
            <Chuva19Status>1</Chuva19Status>
            <Chuva20Status>1</Chuva20Status>
            <Chuva21Status>1</Chuva21Status>
            <Chuva22Status>1</Chuva22Status>
            <Chuva23Status>1</Chuva23Status>
            <Chuva24Status>1</Chuva24Status>
            <Chuva25Status>1</Chuva25Status>
            <Chuva26Status>1</Chuva26Status>
            <Chuva27Status>1</Chuva27Status>
            <Chuva28Status>1</Chuva28Status>
            <Chuva29Status>1</Chuva29Status>
            <Chuva30Status>1</Chuva30Status>
            <Chuva31Status>1</Chuva31Status>
            <DataIns>2015-08-04 00:00:00</DataIns>
         </SerieHistorica>
      </DocumentElement>
   </diffgr:diffgram>
</DataTable>

我正在使用的代码:

xml.doc = content(xml)

##Extract data from list object
xml.list <- xml2::as_list(xml.doc) 
xml.data <- xml.list$DataTable$diffgram$`DocumentElement`

station.data <- matrix(ncol=length(xml.data),nrow = 31) 

if (length(is.na(station.data))==31) {
  next
  print("Skipping this iteration")
} #if no data for this station go onto next iteration
for (j in 1:length(xml.data)) { #j for each record, rows 1:LOD
  month <- xml.data[[j]] 
  df <- as.data.frame(unlist(month[14:44])) 
  if(length(df)==0) next
  station.data[1:length(df[,1]),j] <- as.numeric(as.character(df[,1]))
} 

上面的代码返回错误位置的NA。应该在7到11之间,并且在27到31之间。

Day Month1  Month2  Month3
1   2.6     0.0     0.0
2   0.5     0.0     39.2
3   0.0     0.0     0.0
4   0.0     14.8    4.0
5   0.0     17.6    0.0
6   18.5    38.6    0.0
7   29.7    9.8     0.0
8   0.0     26.2    0.0
9   1.8     29.6    0.0
10  0.0     13.0    0.0
11  0.0     0.0     0.0
12  0.0     0.0     0.0
13  0.0     0.0     0.0
14  0.0     0.0     0.0
15  0.0     0.0     0.0
16  14.8    12.9    0.0
17  1.0     0.0     0.0
18  5.7     14.8    0.0
19  44.8    0.0     0.0
20  1.1     0.0     0.0
21  0.0     0.0     0.0
22  0.0     0.0     0.5
23  0.0     0.0     0.0
24  0.0     0.0     4.3
25  0.0     0.0     15.1
26  14.4    14.0    47.6
27  NA      1.8     7.8
28  NA      11.4    2.0
29  NA      NA      2.2
30  NA      NA      0.0
31  NA      NA      0.0

有什么线索可以解决这个问题吗?

0 个答案:

没有答案