プロが教える店舗&オフィスのセキュリティ対策術

VB6にてcreateNodeを使ってXMLを作成しようとしています。
シンプルなXMLは作成できたのですがAttributeを持ったNodeの追加方法が分かりません。
下記のようなXMLファイルを作成しようとしています。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<setting name="Color_Unknown_On" serializeAs="String">
<value>True</value>
</setting>
</userSettings>
</configuration>

ご教授お願いいたします。

A 回答 (2件)

'VBAでのみテスト


'参照設定でMicrosoft XML,v6.0を追加してある。

Option Explicit

Sub createXML()

Dim document As MSXML2.DOMDocument60

Dim configuration As MSXML2.IXMLDOMElement
Dim userSettings As MSXML2.IXMLDOMElement
Dim setting As MSXML2.IXMLDOMElement
Dim Value As MSXML2.IXMLDOMElement

Dim nsURI As String


'ASP.NET等のconfiguration要素は
'http://schemas.microsoft.com/.NetConfiguration/v …
'だが,質問文に書かれていないので以下の記述とは違うのだろう
'nsURI = "http://schemas.microsoft.com/.NetConfiguration/v …
'ちなみにcreateElementNSメソッドは存在しないため,名前空間に属する要素・属性等はcreateNodeで生成しないとダメっぽい。

'空白文字類の個数等は質問文と一致させない。

nsURI = ""
Set document = New MSXML2.DOMDocument60
Set configuration = document.createNode(MSXML2.NODE_ELEMENT, "Configuration", nsURI)
Set userSettings = document.createNode(MSXML2.NODE_ELEMENT, "userSettings", nsURI)

Set setting = document.createNode(MSXML2.NODE_ELEMENT, "setting", nsURI)

'括弧をつけてはダメらしい。

setting.setAttribute "name", "Color_Unknown_On"
setting.setAttribute "serializeAs", "String"

Set Value = document.createNode(MSXML2.NODE_ELEMENT, "value", nsURI)
Value.Text = "True"

setting.appendChild Value

userSettings.appendChild setting
configuration.appendChild userSettings

document.appendChild configuration
document.Save "hoge.xml"

'この辺,各変数にNothing入れる必要あるかもしれないけど
'よくわからんので放置

End Sub

この回答への補足

himajin100000さん

ありがとうございます。
早速、教えていただいたコードで試してみました。
XMLファイルを作ることが出来ました。

ただ、作成されたXMLファイルを見ると改行がなく、すべて1行に出力されていました。
各Nodeで改行とタブ(インテント)を入れることは可能でしょうか?

<Configuration><userSettings><setting name="Color_Unknown_On" serializeAs="String"><value>True</value></setting></userSettings></Configuration>
  ↓
<Configuration>
  <userSettings>
    <setting name="Color_Unknown_On" serializeAs="String">
      <value>True</value>
    </setting>
  </userSettings>
</Configuration>

補足日時:2008/07/02 16:28
    • good
    • 0

いやあ、参った参った。


そのままでは,だめっぽいっすね

http://msdn.microsoft.com/en-us/library/ms753769 …

>Example

If your DOMDocument contains unformatted XML, you might want to format it before saving. There is no flag in DOMDocument that can be set to indent your XML. In this situation, you might want to use the SAX writer to produce the resulting XML.

The following Microsoft Visual Basic example demonstrates how to perform DOM-to-SAX conversion to use the SAX writer's indent property. It then saves the output from the writer into a file in the application folder.

DOM単体じゃできないから,下のコードでSAXに移行して,そっちでindentプロパティ書け、とかかかれてます。

#最初はXSLTのoutputプロパティ使うかなーとか考えてたが,
#サンプルコードがなくて自力で書くのが面倒くさそうなので却下
    • good
    • 0
この回答へのお礼

himajin100000さま

ありがとうございました。
私もDOMのIndentを設定するPropertyを探したのですが見つかりませんでした。DOMには無いんですね。。。

XMLはTextファイルなのでhimajin100000さまに教えていただいた方法で出力して、後はFSOで修正します。

himajin100000さまのおかげで解決できそうです。
ありがとうございました。

お礼日時:2008/07/08 15:55

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています