Excel VBAでオブジェクト指向プログラミング(XMLドキュメント編)
■XMLとは
XMLはeXtensible Markup Languageの略称で、直訳すれば「拡張可能なマークアップ言語」ということになります。
HTMLと同様に「<タグ>テキスト」という形式で記述しますが、タグを自由に定義でき、データを木構造で表現する点が特徴です。
筆者自身はXMLファイルに接する機会がないため、身近なところにあるXMLファイルとして、Excelファイルを題材にして中身を見てみたいと思います。
Excelファイルの内部構造
Excelで新規ブックを開き、そのまま「Book1.xlsx」として保存し、拡張子をzipに変えて「Book1.zip」にして解凍すると、下図のような構造になっています(※)。
(※)Office2003からXML形式が採用されていますので、WordファイルやPowerPointファイルも拡張子をzipに変えて解凍すると、XMLファイルを参照できます。
「xl」フォルダの下の「worksheets」フォルダの中に「sheet1.xml」というXMLファイルがありますが、これがExcelワークブックの「Sheet1」シートにあたります。
(Book1.zipの内容)
「sheet1.xml」をテキスト・エディタで開くと、以下のような内容になっており、全体が大きく<?xml~?>と<worksheet~>~</worksheet>の2つの部分から構成されていることがわかります。
(sheet1.xmlの内容)
- <?xml version="1.0″ encoding="UTF-8″ standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006″ mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><dimension ref="A1″/><sheetViews><sheetView tabSelected="1″ workbookViewId="0″/></sheetViews><sheetFormatPr defaultRowHeight="13.5″ x14ac:dyDescent="0.15″/><sheetData/><phoneticPr fontId="1″/><pageMargins left="0.7″ right="0.7″ top="0.75″ bottom="0.75″ header="0.3″ footer="0.3″/></worksheet>
次項ではDOMDocumentオブジェクトを使用して、XMLファイルの内容を表示するExcelツールを紹介します。
DOMDocumentオブジェクトとは
DOMDocumentオブジェクトは、MicrosoftのXMLドキュメント・オブジェクト・モデル(DOM)に含まれているオブジェクトで、XML文書に関する機能が実装されています。
DOMDocumentオブジェクトの下位オブジェクト(サブクラス)は下表の通りですが、本項では後述するサンプル・プログラムで使用しているDOMDocumentオブジェクト、IXMLDOMNodeListオブジェクト、IXMLDOMNodeオブジェクトについて概要を説明します。
XML文書は木構造になっているため、文書ツリーのルートはDOMDocumentオブジェクト、要素(ノード)はIXMLDOMNodeListオブジェクトとIXMLDOMNodeオブジェクトで参照します。
⇒詳細はMicrosoftのサイト(XMLドキュメント・オブジェクト・モデル(DOM))を参照して下さい。
DOMDocumentオブジェクトの階層構造
HTMLDocumentオブジェクトの下位オブジェクト(サブクラス)は下表の通りです。
DOMDocument60オブジェクト | |||||||||||
IXMLDOMAttributeオブジェクト | |||||||||||
IXMLDOMCDATASectionオブジェクト | |||||||||||
IXMLDOMCommentオブジェクト | |||||||||||
IXMLDOMDocumentオブジェクト | |||||||||||
IXMLDOMDocumentFragmentオブジェクト | |||||||||||
IXMLDOMDocumentTypeオブジェクト | |||||||||||
IXMLDOMElementオブジェクト | |||||||||||
IXMLDOMEntityReferenceオブジェクト | |||||||||||
IXMLDOMImplementationオブジェクト | |||||||||||
IXMLDOMNamedNodeMapオブジェクト | |||||||||||
IXMLDOMNodeオブジェクト | |||||||||||
IXMLDOMNodeListオブジェクト | |||||||||||
IXMLDOMParseErrorオブジェクト | |||||||||||
IXMLDOMProcessingInstructionオブジェクト | |||||||||||
IXMLDOMTextオブジェクト |
DOMDocumentオブジェクトのメソッドおよびプロパティ
DOMDocumentオブジェクトで提供されているメソッドおよびプロパティは、下表の通りです。
(DOMDocumentオブジェクトのメソッド)
abortメソッド | |||||||||||
説明 | abort an asynchronous download | ||||||||||
定義 | Sub abort() | ||||||||||
appendChildメソッド | |||||||||||
説明 | append a child node | ||||||||||
定義 | Function appendChild(newChild As IXMLDOMNode) As IXMLDOMNode | ||||||||||
cloneNodeメソッド | |||||||||||
説明 | clones a new node | ||||||||||
定義 | Function cloneNode(deep As Boolean) As IXMLDOMNode | ||||||||||
createAttributeメソッド | |||||||||||
説明 | create an attribute node | ||||||||||
定義 | Function createAttribute(name As String) As IXMLDOMAttribute | ||||||||||
createCDATASectionメソッド | |||||||||||
説明 | create a CDATA section node | ||||||||||
定義 | Function createCDATASection(data As String) As IXMLDOMCDATASection | ||||||||||
createCommentメソッド | |||||||||||
説明 | create a comment node | ||||||||||
定義 | Function createComment(data As String) As IXMLDOMComment | ||||||||||
createDocumentFragmentメソッド | |||||||||||
説明 | create a DocumentFragment node | ||||||||||
定義 | Function createDocumentFragment() As IXMLDOMDocumentFragment | ||||||||||
createElementメソッド | |||||||||||
説明 | create an Element node | ||||||||||
定義 | Function createElement(tagName As String) As IXMLDOMElement | ||||||||||
createEntityReferenceメソッド | |||||||||||
説明 | create an entity reference node | ||||||||||
定義 | Function createEntityReference(name As String) As IXMLDOMEntityReference | ||||||||||
createNodeメソッド | |||||||||||
説明 | create a node of the specified node type and name | ||||||||||
定義 | Function createNode(type As Variant, name As String, namespaceURI As String) As IXMLDOMNode | ||||||||||
createProcessingInstructionメソッド | |||||||||||
説明 | create a processing instruction node | ||||||||||
定義 | Function createProcessingInstruction(target As String, data As String) As IXMLDOMProcessingInstruction | ||||||||||
createTextNodeメソッド | |||||||||||
説明 | create a text node | ||||||||||
定義 | Function createTextNode(data As String) As IXMLDOMText | ||||||||||
getElementsByTagNameメソッド | |||||||||||
説明 | build a list of elements by name | ||||||||||
定義 | Function getElementsByTagName(tagName As String) As IXMLDOMNodeList | ||||||||||
getPropertyメソッド | |||||||||||
説明 | get the value of the named property | ||||||||||
定義 | Function getProperty(name As String) As Variant | ||||||||||
hasChildNodesメソッド | |||||||||||
説明 | provides a fast way to determine whether a node has children | ||||||||||
定義 | Function hasChildNodes() As Boolean | ||||||||||
importNodeメソッド | |||||||||||
説明 | clone node such that clones ownerDocument is this document | ||||||||||
定義 | Function importNode(node As IXMLDOMNode, deep As Boolean) As IXMLDOMNode | ||||||||||
insertBeforeメソッド | |||||||||||
説明 | insert a child node | ||||||||||
定義 | Function insertBefore(newChild As IXMLDOMNode, refChild As Variant) As IXMLDOMNode | ||||||||||
loadメソッド | |||||||||||
説明 | load document from the specified XML source | ||||||||||
定義 | Function load(xmlSource As Variant) As Boolean | ||||||||||
loadXMLメソッド | |||||||||||
説明 | load the document from a string | ||||||||||
定義 | Function loadXML(bstrXML As String) As Boolean | ||||||||||
nodeFromIDメソッド | |||||||||||
説明 | retrieve node from it’s ID | ||||||||||
定義 | Function nodeFromID(idString As String) As IXMLDOMNode | ||||||||||
removeChildメソッド | |||||||||||
説明 | remove a child node | ||||||||||
定義 | Function removeChild(childNode As IXMLDOMNode) As IXMLDOMNode | ||||||||||
replaceChildメソッド | |||||||||||
説明 | replace a child node | ||||||||||
定義 | Function replaceChild(newChild As IXMLDOMNode, oldChild As IXMLDOMNode) As IXMLDOMNode | ||||||||||
saveメソッド | |||||||||||
説明 | save the document to a specified destination | ||||||||||
定義 | Sub save(destination As Variant) | ||||||||||
selectNodesメソッド | |||||||||||
説明 | execute query on the subtree | ||||||||||
定義 | Function selectNodes(queryString As String) As IXMLDOMNodeList | ||||||||||
selectSingleNodeメソッド | |||||||||||
説明 | execute query on the subtree | ||||||||||
定義 | Function selectSingleNode(queryString As String) As IXMLDOMNode | ||||||||||
setPropertyメソッド | |||||||||||
説明 | set the value of the named property | ||||||||||
定義 | Sub setProperty(name As String, value As Variant) | ||||||||||
transformNodeメソッド | |||||||||||
説明 | apply the stylesheet to the subtree | ||||||||||
定義 | Function transformNode(stylesheet As IXMLDOMNode) As String | ||||||||||
transformNodeToObjectメソッド | |||||||||||
説明 | apply the stylesheet to the subtree, returning the result through a document or a stream | ||||||||||
定義 | Sub transformNodeToObject(stylesheet As IXMLDOMNode, outputObject As Variant) | ||||||||||
validateメソッド | |||||||||||
説明 | perform runtime validation on the currently loaded XML document | ||||||||||
定義 | Function validate() As IXMLDOMParseError | ||||||||||
validateNodeメソッド | |||||||||||
説明 | perform runtime validation on the currently loaded XML document node | ||||||||||
定義 | Function validateNode(node As IXMLDOMNode) As IXMLDOMParseError |
(DOMDocumentオブジェクトのプロパティ)
asyncプロパティ | |||||||||||
説明 | flag for asynchronous download | ||||||||||
定義 | Property async() As Boolean | ||||||||||
attributesプロパティ(読み取り専用) | |||||||||||
説明 | the collection of the node’s attributes | ||||||||||
定義 | Property attributes() As IXMLDOMNamedNodeMap | ||||||||||
baseNameプロパティ(読み取り専用) | |||||||||||
説明 | the base name of the node (nodename with the prefix stripped off) | ||||||||||
定義 | Property baseName() As String | ||||||||||
childNodesプロパティ(読み取り専用) | |||||||||||
説明 | the collection of the node’s children | ||||||||||
定義 | Property childNodes() As IXMLDOMNodeList | ||||||||||
dataTypeプロパティ | |||||||||||
説明 | the data type of the node | ||||||||||
定義 | Property dataType() As Variant | ||||||||||
definitionプロパティ(読み取り専用) | |||||||||||
説明 | pointer to the definition of the node in the DTD or schema | ||||||||||
定義 | Property definition() As IXMLDOMNode | ||||||||||
doctypeプロパティ(読み取り専用) | |||||||||||
説明 | node corresponding to the DOCTYPE | ||||||||||
定義 | Property doctype() As IXMLDOMDocumentType | ||||||||||
documentElementプロパティ | |||||||||||
説明 | the root of the tree | ||||||||||
定義 | Property documentElement() As IXMLDOMElement | ||||||||||
firstChildプロパティ(読み取り専用) | |||||||||||
説明 | first child of the node | ||||||||||
定義 | Property firstChild() As IXMLDOMNode | ||||||||||
implementationプロパティ(読み取り専用) | |||||||||||
説明 | info on this DOM implementation | ||||||||||
定義 | Property implementation() As IXMLDOMImplementation | ||||||||||
lastChildプロパティ(読み取り専用) | |||||||||||
説明 | last child of the node | ||||||||||
定義 | Property lastChild() As IXMLDOMNode | ||||||||||
namespacesプロパティ(読み取り専用) | |||||||||||
説明 | A collection of all namespaces for this document | ||||||||||
定義 | Property namespaces() As IXMLDOMSchemaCollection | ||||||||||
namespaceURIプロパティ(読み取り専用) | |||||||||||
説明 | the URI for the namespace applying to the node | ||||||||||
定義 | Property namespaceURI() As String | ||||||||||
nextSiblingプロパティ(読み取り専用) | |||||||||||
説明 | right sibling of the node | ||||||||||
定義 | Property nextSibling() As IXMLDOMNode | ||||||||||
nodeNameプロパティ(読み取り専用) | |||||||||||
説明 | name of the node | ||||||||||
定義 | Property nodeName() As String | ||||||||||
nodeTypeプロパティ(読み取り専用) | |||||||||||
説明 | the node’s type | ||||||||||
定義 | Property nodeType() As tagDOMNodeType | ||||||||||
nodeTypedValueプロパティ | |||||||||||
説明 | get the strongly typed value of the node | ||||||||||
定義 | Property nodeTypedValue() As Variant | ||||||||||
nodeTypeStringプロパティ(読み取り専用) | |||||||||||
説明 | the type of node in string form | ||||||||||
定義 | Property nodeTypeString() As String | ||||||||||
nodeValueプロパティ | |||||||||||
説明 | value stored in the node | ||||||||||
定義 | Property nodeValue() As Variant | ||||||||||
ondataavailableプロパティ | |||||||||||
説明 | register an ondataavailable event handler | ||||||||||
定義 | Property ondataavailable() As Variant | ||||||||||
onreadystatechangeプロパティ | |||||||||||
説明 | register a readystatechange event handler | ||||||||||
定義 | Property onreadystatechange() As Variant | ||||||||||
ontransformnodeプロパティ | |||||||||||
説明 | register an ontransformnode event handler | ||||||||||
定義 | Property ontransformnode() As Variant | ||||||||||
ownerDocumentプロパティ(読み取り専用) | |||||||||||
説明 | document that contains the node | ||||||||||
定義 | Property ownerDocument() As IXMLDOMDocument | ||||||||||
parentNodeプロパティ(読み取り専用) | |||||||||||
説明 | parent of the node | ||||||||||
定義 | Property parentNode() As IXMLDOMNode | ||||||||||
parsedプロパティ(読み取り専用) | |||||||||||
説明 | has sub-tree been completely parsed | ||||||||||
定義 | Property parsed() As Boolean | ||||||||||
parseErrorプロパティ(読み取り専用) | |||||||||||
説明 | get the last parser error | ||||||||||
定義 | Property parseError() As IXMLDOMParseError | ||||||||||
prefixプロパティ(読み取り専用) | |||||||||||
説明 | the prefix for the namespace applying to the node | ||||||||||
定義 | Property prefix() As String | ||||||||||
preserveWhiteSpaceプロパティ | |||||||||||
説明 | indicates whether the parser preserves whitespace | ||||||||||
定義 | Property preserveWhiteSpace() As Boolean | ||||||||||
previousSiblingプロパティ(読み取り専用) | |||||||||||
説明 | left sibling of the node | ||||||||||
定義 | Property previousSibling() As IXMLDOMNode | ||||||||||
readyStateプロパティ(読み取り専用) | |||||||||||
説明 | get the state of the XML document | ||||||||||
定義 | Property readyState() As Long | ||||||||||
resolveExternalsプロパティ | |||||||||||
説明 | indicates whether the parser resolves references to external DTD/Entities/Schema | ||||||||||
定義 | Property resolveExternals() As Boolean | ||||||||||
schemasプロパティ | |||||||||||
説明 | The associated schema cache | ||||||||||
定義 | Property schemas() As Variant | ||||||||||
specifiedプロパティ(読み取り専用) | |||||||||||
説明 | indicates whether node is a default value | ||||||||||
定義 | Property specified() As Boolean | ||||||||||
textプロパティ | |||||||||||
説明 | text content of the node and subtree | ||||||||||
定義 | Property text() As String | ||||||||||
urlプロパティ(読み取り専用) | |||||||||||
説明 | get the URL for the loaded XML document | ||||||||||
定義 | Property url() As String | ||||||||||
validateOnParseプロパティ | |||||||||||
説明 | indicates whether the parser performs validation | ||||||||||
定義 | Property validateOnParse() As Boolean | ||||||||||
xmlプロパティ(読み取り専用) | |||||||||||
説明 | return the XML source for the node and each of its descendants | ||||||||||
定義 | Property xml() As String |
IXMLDOMNodeListオブジェクトのメソッドおよびプロパティ
IXMLDOMNodeListオブジェクトで提供されているメソッドおよびプロパティは、下表の通りです。
(IXMLDOMNodeListオブジェクトのメソッドおよびプロパティ)
itemプロパティ(既定)(読み取り専用) | |||||||||||
説明 | collection of nodes | ||||||||||
定義 | Property item(index As Long) As IXMLDOMNode | ||||||||||
lengthプロパティ(読み取り専用) | |||||||||||
説明 | number of nodes in the collection | ||||||||||
定義 | Property length() As Long | ||||||||||
nextNodeメソッド | |||||||||||
説明 | get next node from iterator | ||||||||||
定義 | Function nextNode() As IXMLDOMNode | ||||||||||
resetメソッド | |||||||||||
説明 | reset the position of iterator | ||||||||||
定義 | Sub reset() |
IXMLDOMNodeオブジェクトのメソッドおよびプロパティ
IXMLDOMNodeオブジェクトで提供されているメソッドおよびプロパティは、下表の通りです。
(IXMLDOMNodeオブジェクトのメソッド)
appendChildメソッド | |||||||||||
説明 | append a child node | ||||||||||
定義 | Function appendChild(newChild As IXMLDOMNode) As IXMLDOMNode | ||||||||||
cloneNodeメソッド | |||||||||||
説明 | clones a new node | ||||||||||
定義 | Function cloneNode(deep As Boolean) As IXMLDOMNode | ||||||||||
hasChildNodesメソッド | |||||||||||
説明 | provides a fast way to determine whether a node has children | ||||||||||
定義 | Function hasChildNodes() As Boolean | ||||||||||
insertBeforeメソッド | |||||||||||
説明 | insert a child node | ||||||||||
定義 | Function insertBefore(newChild As IXMLDOMNode, refChild As Variant) As IXMLDOMNode | ||||||||||
removeChildメソッド | |||||||||||
説明 | remove a child node | ||||||||||
定義 | Function removeChild(childNode As IXMLDOMNode) As IXMLDOMNode | ||||||||||
replaceChildメソッド | |||||||||||
説明 | replace a child node | ||||||||||
定義 | Function replaceChild(newChild As IXMLDOMNode, oldChild As IXMLDOMNode) As IXMLDOMNode | ||||||||||
selectNodesメソッド | |||||||||||
説明 | execute query on the subtree | ||||||||||
定義 | Function selectNodes(queryString As String) As IXMLDOMNodeList | ||||||||||
selectSingleNodeメソッド | |||||||||||
説明 | execute query on the subtree | ||||||||||
定義 | Function selectSingleNode(queryString As String) As IXMLDOMNode | ||||||||||
transformNodeメソッド | |||||||||||
説明 | apply the stylesheet to the subtree | ||||||||||
定義 | Function transformNode(stylesheet As IXMLDOMNode) As String | ||||||||||
transformNodeToObjectメソッド | |||||||||||
説明 | apply the stylesheet to the subtree, returning the result through a document or a stream | ||||||||||
定義 | Sub transformNodeToObject(stylesheet As IXMLDOMNode, outputObject As Variant) |
(IXMLDOMNodeオブジェクトのプロパティ)
attributesプロパティ(読み取り専用) | |||||||||||
説明 | the collection of the node’s attributes | ||||||||||
定義 | Property attributes() As IXMLDOMNamedNodeMap | ||||||||||
baseNameプロパティ(読み取り専用) | |||||||||||
説明 | the base name of the node (nodename with the prefix stripped off) | ||||||||||
定義 | Property baseName() As String | ||||||||||
childNodesプロパティ(読み取り専用) | |||||||||||
説明 | the collection of the node’s children | ||||||||||
定義 | Property childNodes() As IXMLDOMNodeList | ||||||||||
dataTypeプロパティ | |||||||||||
説明 | the data type of the node | ||||||||||
定義 | Property dataType() As Variant | ||||||||||
definitionプロパティ(読み取り専用) | |||||||||||
説明 | pointer to the definition of the node in the DTD or schema | ||||||||||
定義 | Property definition() As IXMLDOMNode | ||||||||||
firstChildプロパティ(読み取り専用) | |||||||||||
説明 | first child of the node | ||||||||||
定義 | Property firstChild() As IXMLDOMNode | ||||||||||
lastChildプロパティ(読み取り専用) | |||||||||||
説明 | last child of the node | ||||||||||
定義 | Property lastChild() As IXMLDOMNode | ||||||||||
namespaceURIプロパティ(読み取り専用) | |||||||||||
説明 | the URI for the namespace applying to the node | ||||||||||
定義 | Property namespaceURI() As String | ||||||||||
nextSiblingプロパティ(読み取り専用) | |||||||||||
説明 | right sibling of the node | ||||||||||
定義 | Property nextSibling() As IXMLDOMNode | ||||||||||
nodeNameプロパティ(読み取り専用) | |||||||||||
説明 | name of the node | ||||||||||
定義 | Property nodeName() As String | ||||||||||
nodeTypeプロパティ(読み取り専用) | |||||||||||
説明 | the node’s type | ||||||||||
定義 | Property nodeType() As tagDOMNodeType | ||||||||||
nodeTypedValueプロパティ | |||||||||||
説明 | get the strongly typed value of the node | ||||||||||
定義 | Property nodeTypedValue() As Variant | ||||||||||
nodeTypeStringプロパティ(読み取り専用) | |||||||||||
説明 | the type of node in string form | ||||||||||
定義 | Property nodeTypeString() As String | ||||||||||
nodeValueプロパティ | |||||||||||
説明 | value stored in the node | ||||||||||
定義 | Property nodeValue() As Variant | ||||||||||
ownerDocumentプロパティ(読み取り専用) | |||||||||||
説明 | document that contains the node | ||||||||||
定義 | Property ownerDocument() As IXMLDOMDocument | ||||||||||
parentNodeプロパティ(読み取り専用) | |||||||||||
説明 | parent of the node | ||||||||||
定義 | Property parentNode() As IXMLDOMNode | ||||||||||
parsedプロパティ(読み取り専用) | |||||||||||
説明 | has sub-tree been completely parsed | ||||||||||
定義 | Property parsed() As Boolean | ||||||||||
prefixプロパティ(読み取り専用) | |||||||||||
説明 | the prefix for the namespace applying to the node | ||||||||||
定義 | Property prefix() As String | ||||||||||
previousSiblingプロパティ(読み取り専用) | |||||||||||
説明 | left sibling of the node | ||||||||||
定義 | Property previousSibling() As IXMLDOMNode | ||||||||||
specifiedプロパティ(読み取り専用) | |||||||||||
説明 | indicates whether node is a default value | ||||||||||
定義 | Property specified() As Boolean | ||||||||||
textプロパティ | |||||||||||
説明 | text content of the node and subtree | ||||||||||
定義 | Property text() As String | ||||||||||
xmlプロパティ(読み取り専用) | |||||||||||
説明 | return the XML source for the node and each of its descendants | ||||||||||
定義 | Property xml() As String |
DOMDocumentオブジェクトを使用して、XMLファイルの内容をExcelシートに表示する処理
ここではDOMDocumentオブジェクトを使用して、XMLファイルの内容をExcelシートに表示するためのサンプルプログラムをご紹介します。
DOMDocumentオブジェクトを使用するための事前準備
DOMDocumentオブジェクトを使用するための事前準備は、以下の3ステップです。
⇒オブジェクトを使用するための事前準備とオブジェクトブラウザによる調査方法については、Excel VBAでオブジェクト指向プログラミング(事前準備編)を参照して下さい。
①ライブラリの参照設定
Visual Basic Editor(VBE)のファイルメニューから[ツール]-[参照設定]を選択し、参照設定ダイアログでMicrosoft XML. v6.0のチェックボックスにチェックを入れ、[OK]ボタンを押下。
②オブジェクト変数の定義
プログラムの宣言部でオブジェクト変数を定義。
- Dim doc As MSXML2.DOMDocument60
③インスタンスの生成
プログラムの処理部でインスタンスを生成。
- Set doc = New MSXML2.DOMDocument60
- 'ここにオブジェクトを使用した処理を記述
- Set doc = Nothing
(参考)「Set doc = New MSXML2.DOMDocument60」の部分を「Set doc = CreateObject(“MSXML2.DOMDocument.6.0")」と記述すれば、ステップ①(ライブラリの参照設定)は不要です。
サンプル・プログラム(1)~XML文書をそのまま表示
DOMDocumentオブジェクトのLoadメソッドでXMLファイルを入力し、XMLプロパティでデータを取得すると、入力ファイルの中身を丸ごと取得できます。
行番号7でDOMDocumentオブジェクトをインスタンス化した後、行番号8でLoadメソッドを使用してXMLファイルを入力し、行番号9でXMLプロパティにより内容を取得しています。
- Dim sht As Worksheet
- Dim doc As MSXML2.DOMDocument60
- Private Sub Sample1()
- Set sht = ActiveSheet
- sht.Cells.Clear
- Set doc = New MSXML2.DOMDocument60
- doc.Load “C:\work\sheet1.xml"
- sht.Cells(1, 1) = doc.xml
- Set doc = Nothing
- End Sub
サンプル・プログラム(2)~XML文書を階層毎に表示
XMLファイルの中身が木構造になっているので、階層がわかるようにカラムをずらしながら表示してみます。
DOMDocumentオブジェクトのLoadメソッドでXMLファイルを入力した後、ChildNodesプロパティでIXMLDOMNodeListオブジェクト(IXMLDOMNodeオブジェクトのコレクションです)を取得し、そこから1つずつIXMLDOMNodeオブジェクトを取出して、XMLプロパティでデータを取得すると、各要素(ノード)の内容を取得できます。
行番号10でDOMDocumentオブジェクトをインスタンス化した後、行番号11でLoadメソッドを使用してXMLファイルを入力し、行番号14でサブプロシジャをCallしています。
情報の取得とExcelシートへの表示はサブプロシジャで行っており、行番号20でノードの個数が0かどうか(IXMLDOMNodeListオブジェクトのLengthプロパティが0かどうか)を判定し、0でなければ行番号21~25の繰返し処理に入ります。
行番号21でIXMLDOMNodeListオブジェクトから1つずつIXMLDOMNodeオブジェクトを取出し、行番号23でXMLプロパティから取得したデータをExcelシートにセットした後、行番号24で再帰呼び出しを行っています。
- Dim sht As Worksheet
- Dim doc As MSXML2.DOMDocument60
- Dim XMLchild As MSXML2.IXMLDOMNode
- Dim rcnt As Integer
- Dim ccnt As Integer
- Private Sub CommandButton1_Click()
- Set sht = ActiveSheet
- sht.Cells.Clear
- Set doc = New MSXML2.DOMDocument60
- doc.Load “C:\work\sheet1.xml"
- rcnt = 0
- ccnt = 0
- Call Sample2_sub(doc)
- Set doc = Nothing
- End Sub
- Private Sub Sample2_sub(XMLparent As Variant)
- ccnt = ccnt + 1
- If XMLparent.ChildNodes.Length <> 0 Then
- For Each XMLchild In XMLparent.ChildNodes
- rcnt = rcnt + 1
- sht.Cells(rcnt, ccnt) = XMLchild.xml
- Call Sample2_sub(XMLchild)
- Next XMLchild
- End If
- ccnt = ccnt – 1
- End Sub
このツールを実行すると、下図のような結果が表示されます
実行結果(イメージ)
<?xml~?> | ||
<worksheet~>~ | </worksheet> | |
<dimension~/> | ||
<sheetViews> | ||
<sheetView~/> | ||
<sheetFormatPr~/> | ||
<sheetData/> | ||
<phoneticPr~/> | ||
<pageMargins~/> |
サンプル・プログラム(3)~XML文書の色々な情報を表示
サンプル・プログラム(2)と同様の処理で、XMLプロパティ以外の情報を表示してみます。
行番号9でDOMDocumentオブジェクトをインスタンス化した後、行番号10でLoadメソッドを使用してXMLファイルを入力し、行番号12でサブプロシジャをCallしています。
情報の取得とExcelシートへの表示はサブプロシジャで行っており、行番号17でノードの個数が0かどうか(IXMLDOMNodeListオブジェクトのLengthプロパティが0かどうか)を判定し、0でなければ行番号18~26の繰返し処理に入ります。
行番号18でIXMLDOMNodeListオブジェクトから1つずつIXMLDOMNodeオブジェクトを取出し、行番号20で子ノードの数、行番号21でBaseNameプロパティ、行番号22でNamespaceURIプロパティ、行番号23でnodeNameプロパティ、行番号24でnodeTypeStringプロパティをExcelシートにセットした後、行番号25で再帰呼び出しを行っています。
- Dim sht As Worksheet
- Dim doc As MSXML2.DOMDocument60
- Dim XMLchild As MSXML2.IXMLDOMNode
- Dim rcnt As Integer
- Private Sub Sample3()
- Set sht = ActiveSheet
- sht.Cells.Clear
- Set doc = New MSXML2.DOMDocument60
- doc.Load “C:\work\sheet1.xml"
- rcnt = 0
- Call Sample3_sub(doc)
- Set doc = Nothing
- End Sub
- Private Sub Sample3_sub(XMLparent As Variant)
- If XMLparent.ChildNodes.Length <> 0 Then
- For Each XMLchild In XMLparent.ChildNodes
- rcnt = rcnt + 1
- sht.Cells(rcnt, 1) = XMLparent.ChildNodes.Length
- sht.Cells(rcnt, 2) = XMLchild.BaseName
- sht.Cells(rcnt, 3) = XMLchild.NamespaceURI
- sht.Cells(rcnt, 4) = XMLchild.nodeName
- sht.Cells(rcnt, 5) = XMLchild.nodeTypeString
- Call Sample3_sub(XMLchild)
- Next XMLchild
- End If
- End Sub
このツールを実行すると、下図のような結果が表示されます
実行結果(イメージ)
2 | xml | xml | processinginstruction | |
2 | worksheet | http://~ | worksheet | element |
6 | dimension | http://~ | dimension | element |
6 | sheetViews | http://~ | sheetViews | element |
1 | sheetView | http://~ | sheetView | element |
6 | sheetFormatPr | http://~ | sheetFormatPr | element |
6 | sheetData | http://~ | sheetData | element |
6 | phoneticPr | http://~ | phoneticPr | element |
6 | pageMargins | http://~ | pageMargins | element |
出版社:インプレス
発売日:2022/3/23
単行本(ソフトカバー):A5判/912ページ
出版社:技術評論社
発売日:2021/1/9
単行本(ソフトカバー):A5判/800ページ
出版社:技術評論社
発売日:2019/11/25
単行本(ソフトカバー):B5変形判/576ページ
ディスカッション
コメント一覧
まだ、コメントがありません