VBS生成XML文件


Dim xmlDoc, rootEl, child1, child2, p

'創建XML文檔
Set xmlDoc = CreateObject("MSXML2.DOMDocument")

'創建根元素並將之加入文檔
Set rootE1=xmlDoc.createElement("BookStore")
xmlDoc.appendChild rootE1

'創建並加入子元素
Set bookchild1=xmlDoc.createElement("book")
Set ISDNAttribute=xmlDoc.createAttribute("ISDN")
ISDNAttribute.text="789456123"
bookchild1.setAttributeNode ISDNAttribute

Set bookchild1_title=xmlDoc.createElement("title")
bookchild1_title.text="C#"
bookchild1.appendChild bookchild1_title

Set bookchild1_author=xmlDoc.createElement("author")
bookchild1_author.text="BXH"
bookchild1.appendChild bookchild1_author

Set bookchild1_price=xmlDoc.createElement("Price")
bookchild1_price.text="29.3"
bookchild1.appendChild bookchild1_price

rootE1.appendChild bookchild1

'創建並加入子元素
Set bookchild2=xmlDoc.createElement("book")
Set ISDNAttribute=xmlDoc.createAttribute("ISDN")
ISDNAttribute.text="789457898"
bookchild2.setAttributeNode ISDNAttribute

Set bookchild2_title=xmlDoc.createElement("title")
bookchild2_title.text="F#"
bookchild2.appendChild bookchild2_title

Set bookchild2_author=xmlDoc.createElement("author")
bookchild2_author.text="mary"
bookchild2.appendChild bookchild2_author

Set bookchild2_price=xmlDoc.createElement("Price")
bookchild2_price.text="78"
bookchild2.appendChild bookchild2_price

rootE1.appendChild bookchild2

'創建 XML processing instruction
'並把它加到根元素之前
Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)

'把文件保存到c目錄
xmlDoc.Save "d:\test.xml"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM