VBA:XML作成

'MDRを読み込んで新規XMLファイルを作成するVBAプログラム
Sub export_xml()

	Dim xdoc As New DOMDocument, xtree As IXMLDOMElement
	Dim PI As IXMLDOMProcessingInstruction
	Dim LOB As IXMLDOMElement, FAM As IXMLDOMElement
	Dim BRAND As IXMLDOMElement
	Dim RS As Recordset
	
	'xml header
	Set PI = xdoc.createProcessingInstruction(“xml", “version='1.0′")
	Call xdoc.appendChild(PI)
	'root tree
	Set xtree = xdoc.createElement(“PRODUCT")
	Call xdoc.appendChild(xtree)
	'get data from database
	Set RS = CurrentDb.OpenRecordset(“select * from xxxx")
	
	Do While Not RS.EOF
		'親
		Set LOB = xdoc.createElement(“LOB")
		Call LOB.setAttribute(“NAME", RS!PRODUCT)
			'子1
			Set FAM = xdoc.createElement(“FAMILY")
			FAM.Text = RS!FAMILY
			Call FAM.setAttribute(“CODE", RS!FAMILY_ID)
			Call LOB.appendChild(FAM)
			'子2
			Set BRAND = xdoc.createElement(“BRAND")
			BRAND.Text = RS!BRAND
			Call BRAND.setAttribute(“CODE", RS!BRAND_CD)
			Call LOB.appendChild(BRAND)
		'親書き出し
		Call xtree.appendChild(LOB)
		'next record
		RS.MoveNext
	Loop
	'db close
	RS.Close
	'xml保存
	xdoc.Save (“c:\temp\test.xml")

End Sub

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です