Option Explicit
Public Function test()
Dim XMLDoc As MSXML2.DOMDocument
Dim RootNode As MSXML2.IXMLDOMElement
Dim Nodes1 As MSXML2.IXMLDOMNodeList
Dim Node1 As MSXML2.IXMLDOMElement
Dim Node2 As MSXML2.IXMLDOMElement
Set XMLDoc = getXML
Set RootNode = XMLDoc.selectSingleNode("Books")
'or
'Set RootNode = XMLDoc.firstChild
Set Nodes1 = RootNode.selectNodes("Book")
For Each Node1 In Nodes1
With Node1
Debug.Print "Title: "; .Attributes.getNamedItem("Title").Text
Set Node2 = .selectSingleNode("Author")
With Node2
Debug.Print "Author: ";
Debug.Print .Attributes.getNamedItem("Name").Text,
Debug.Print "Age: "; .Attributes.getNamedItem("Age").Text,
Debug.Print "Country: "; .Attributes.getNamedItem("Country").Text
Debug.Print "Comment: "; .Text
End With
Debug.Print "Description: "; .Text
Debug.Print "Pages "; .Attributes.getNamedItem("Pages").Text
Debug.Print
End With
Next
End Function
Public Function getXML() As MSXML2.DOMDocument
Dim XMLDoc As MSXML2.DOMDocument
Dim RootNode As MSXML2.IXMLDOMElement
Dim Node1 As MSXML2.IXMLDOMElement
Dim Node2 As MSXML2.IXMLDOMElement
Set XMLDoc = New MSXML2.DOMDocument
XMLDoc.async = False
Set RootNode = XMLDoc.appendChild(XMLDoc.createElement("Books"))
Set Node1 = RootNode.appendChild(XMLDoc.createElement("Book"))
With Node1
Call .setAttribute("Title", "100 years without water")
Call .setAttribute("Pages", 100)
.Text = "This book about..."
Set Node2 = .appendChild(XMLDoc.createElement("Author"))
With Node2
Call .setAttribute("Name", "Vasja Pupkin")
Call .setAttribute("Age", 12)
Call .setAttribute("Country", "Ukraine")
.Text = "This author is..."
End With
End With
Set Node1 = RootNode.appendChild(XMLDoc.createElement("Book"))
With Node1
Call .setAttribute("Title", "1000 kilometers without foot")
Call .setAttribute("Pages", 300)
.Text = "This book about..."
Set Node2 = .appendChild(XMLDoc.createElement("Author"))
With Node2
Call .setAttribute("Name", "Kolja Zadristchev")
Call .setAttribute("Age", 13)
Call .setAttribute("Country", "USA")
.Text = "This author is..."
End With
End With
Set Node1 = RootNode.appendChild(XMLDoc.createElement("Book"))
With Node1
Call .setAttribute("Title", "10000 programm lines without head")
Call .setAttribute("Pages", 150)
.Text = "This book about..."
Set Node2 = .appendChild(XMLDoc.createElement("Author"))
With Node2
Call .setAttribute("Name", "Petja Balabanov")
Call .setAttribute("Age", 17)
Call .setAttribute("Country", "Japon")
.Text = "This author is..."
End With
End With
Set getXML = XMLDoc
If (Not Node2 Is Nothing) Then Set Node2 = Nothing
If (Not Node1 Is Nothing) Then Set Node1 = Nothing
If (Not RootNode Is Nothing) Then Set RootNode = Nothing
If (Not XMLDoc Is Nothing) Then Set XMLDoc = Nothing
End Function
jetcom писал(а):Все работает и интуитивно вроде бы понятно, но все-равно много белых пятен, и почитать на эту тему чего-нить хочется!
jetcom писал(а):XML в твоем коде создается виртуально, а каким образом его создать на диске и потом "прощупать"?........
XMLDoc.save "<path>"
Сейчас этот форум просматривают: Yandex-бот и гости: 131