LoginSignup
3
0

More than 5 years have passed since last update.

RとVBAでXBRLのデータを触った

Last updated at Posted at 2017-06-30

ゼミの後輩が内部統制のことを研究テーマとした。
そのため、XBRLの内部統制報告書をダウンロードしてあげた。
ダウンロードはRで実施した。
以下コードを提示する

library(XML)
library(RCurl)
codelist<-read.csv("Book1.csv",header=FALSE)
for (it in 1:nrow(codelist)){
  namelistitem <-codelist[it,1]
  QueryURL<-paste("http://resource.ufocatch.com/atom/edinetx/query/", namelistitem,sep = "")
  objQuery<-getURL(QueryURL)
  objXML<-xmlParse(objQuery,encoding="utf-8")
  objXML.namespaces <- xmlNamespaceDefinitions(objXML,simplify=TRUE)
  names(objXML.namespaces)[ names(objXML.namespaces)=="" ] <- "default"
  nodes.entry <- getNodeSet(objXML,"//default:entry",namespaces=objXML.namespaces)
  for(node in nodes.entry){ # <entry>タグをひとつずつ処理
    lst.temp <- list()
    # 提出書類のタイトルを取得→要素ノード:title の内容を取得
    title.value <- xpathSApply(node,path="default:title",fun=xmlValue,namespaces=objXML.namespaces)
    # 提出書類のタイトルに「内部統制報告書」を含むか否かを判定
    title.split<-unlist(strsplit(title.value, " "))[2]
    title.split2<-unlist(strsplit(title.split,""))[1]
    if( title.split2=="内"){
      htmlurl <- xpathSApply(node,path="default:link[@type='text/html']/@href",namespaces=objXML.namespaces)
      htmlstringlist<-unlist(strsplit(htmlurl,"_")) 
      download.file(htmlurl[1],destfile = paste(namelistitem,htmlstringlist[5],"header",".html"),quiet = TRUE)
      download.file(htmlurl[2],destfile = paste(namelistitem,htmlstringlist[5],"houbun",".html"),quiet = TRUE)    
      print(title.value)      
    }
  }
  it=it+1
}

内容の抽出はVBAでやった。
以下コードを提示する

Public Sub sample1()
    Dim XMLDocument As MSXML2.DOMDocument
    Dim xmlDate As IXMLDOMNode
    Dim xmlCustomer As IXMLDOMNode
    Dim xmlDataNode As IXMLDOMNode
    Dim i As Long
    Dim htmlname As String
For i = 1 To 12558
htmlname = Cells(i, 1).Value
On Error GoTo ERROR_

    'MSXMLオブジェクトを生成し、xmlファイルをロード
    Set XMLDocument = New MSXML2.DOMDocument
    XMLDocument.async = False
    XMLDocument.Load (htmlname)
    If (XMLDocument.parseError.ErrorCode <> 0) Then 'ロード失敗
        Debug.Print XMLDocument.parseError.reason   'エラー内容を出力
        GoTo ERROR_
    End If



    '<DATA>ノードデータを取得
    Set xmlDataNode = XMLDocument.SelectSingleNode("//body")

    '<DATA>ノードの子要素をループで抽出
    Dim Node As IXMLDOMNode
    For Each Node In xmlDataNode.ChildNodes
        For nlen = 0 To Node.ChildNodes.Length - 1
            Cells(i, nlen + 4) = Node.ChildNodes(nlen).Text
        Next
    Next

ERROR_:

    '各オブジェクトの開放
    If Not XMLDocument Is Nothing Then Set XMLDocument = Nothing
    If Not xmlDate Is Nothing Then Set xmlDate = Nothing
    If Not xmlCustomer Is Nothing Then Set xmlCustomer = Nothing
    If Not xmlDataNode Is Nothing Then Set xmlDataNode = Nothing

Next
End Sub

参考になったURLは後ほど追加する。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0