2017-03-06 7 views
0

XMLファイル内のすべての要素を繰り返し処理し、テキストファイルを使用して要素に値を割り当てます。すべてのXML要素をループし、テキストファイルの入力に基づいてXMLファイルを追加します。

project_template.xml

<project baseDir="" outputDir=""> 
    <rule inherit="" preset="" pattern="" /> 
</project> 

test.txt

baseDir="test1" 
outputDir="test2" 
inherit="test3" 
preset="test4" 
pattern="true"

Project.vbs

Option Explicit 
Dim arrFileLines(), final(45) 
Dim i, objFSO, objFile, l, index, finalString 
i = 0 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\Users\1021752\Desktop\project\test.txt", 1) 
Do Until objFile.AtEndOfStream 
    ReDim Preserve arrFileLines(i) 
    arrFileLines(i) = objFile.ReadLine 
    i = i + 1 
Loop 
objFile.Close 

Const NODE_ELEMENT = 1 
Const CONFUSER_NS = "http://confuser.codeplex.com" 

Dim doc, moduleElem, args, arg 

Set args = WScript.Arguments 
Set doc = CreateObject("Msxml2.DOMDocument") 

doc.Async = False 
doc.Load "project_template.xml" 

If doc.ParseError.ErrorCode Then 
    WScript.Echo doc.ParseError 
    WScript.Quit 1 
End If 

For l = LBound(arrFileLines) To UBound(arrFileLines) Step 1 
    index = InStr(1, arrFileLines(l), "=") 
    final(l) = Right(arrFileLines(l), Len(arrFileLines(l)) - index) 
Next 
doc.DocumentElement.SetAttribute "baseDir", final(0) 'here I am manually assigning the value to the "baseDir" 
doc.DocumentElement.SetAttribute "outputDir", final(1) 
doc.DocumentElement.SetAttribute "inherit", final(2) 
doc.DocumentElement.SetAttribute "preset", final(3) 
doc.DocumentElement.SetAttribute "pattern", final(4) 
doc.Save "project.xml" 

所望の出力:

<project baseDir="test1" outputDir="test2" > 
    <rule inherit="test3" preset="test4" pattern="true" /> 
</project> 

XMLファイル内のすべての要素を反復処理して名前を返すようにします。

答えて

0

私はあなたのパラメータがdictionaryにファイルを読んで推薦:

Set fso = CreateObject("Scripting.FileSystemObject") 

Set d = CreateObject("Scripting.Dictionary") 
Set f = fso.OpenTextFile("C:\Users\1021752\Desktop\project\test.txt") 
Do Until f.AtEndOfStream 
    arr = Split(f.ReadLine, "=", 2) 
    d(arr(0)) = Replace(arr(1), """", "") 
Loop 
f.Close 

また、あなたのコードではなく、個々のノードの属性を更新するXMLデータのルートノードにすべての属性を追加します。

Sub UpdateAttributes(node, values) 
    For Each attr In node.Attributes 
    If values.Exists(attr.Name) Then node.SetAttribute attr.Name, values(attr.Name) 
    Next 

    For Each child In node.ChildNodes 
    UpdateAttributes child, values 
    Next 
End Sub 

は、XMLのルートノードと辞書との手順を起動します。何が必要、その子ノードのすべてに対して同じ処理を行い、現在のノードに存在する属性を更新し、再帰的手続きでありますノード内の各生徒用

UpdateAttributes doc.DocumentElement, d 
+0

Iはnode.Attributes node.SetAttributeのATTRの各ATTRについて Iは、IF文 'サブUpdateAttributes(ノード値)を除去し、ビットのコードを変更し、値(ATTR) 次に .ChildNodes 更新eAttributes child、values 次 End Sub'このメソッドを実装して実行すると、型不一致エラー800A000Dがスローされています。あなたは解決策が見つからなかったので解決するのを助けてくれますか? –

+0

'attr'だけでなく、' attr.Name'を使う必要があります。更新された回答をご覧ください。 –

関連する問題