The XML format is used a lot and often to changing info between applications or as configuration files on which it is indicated how pages/tables must be created or something else.
So it is very useful know how to manage, Read and Write them; let's see how.
XML files constist of data grouped per type using a hierarchy. Suppose to have, for example, an XML structured in this manner:
<?xml version="1.0"?>
<Root>
<ElencoElementi>
<Elemento>
<IDElemento>
<string>"1"</string>
</IDElemento>
<ValoreElemento>
<string>"Libro"</string>
</ValoreElemento>
</Elemento>
</ElencoElementi>
</Root>
Legend (because it comes from the Italian site):
ElencoElementi = ElementsList
Elemento = Element
IDElemento = IDElement
ValoreElemento = ElementValue
Libro = Book
Suppose we'd like to put another Element who will have ID = "2" and the Value will be "Penna" (Pen). Suppose also that the file name is "element.xml" and it is stored in c:\temp. We'll write this code:
'************************************************
'***************** M A I N ************************
'************************************************
Dim objXMLDoc, oRootElement, oNodeElencoElementi, oNodeElemento
Dim oNodeElementoId, oNodeElementoVal, oNodeStrId, oNodeStrVal
Const myVar = "c:\temp\element.xml"
Const ID_ToAdd = "2"
Const ValueToAdd = "Penna"
'MS XML DOM
set objXMLDoc=CreateObject("Microsoft.XMLDOM")
objXMLDoc.async="false"
'loading xml file ---> myVar
objXMLDoc.load(myVar)
'the root element of the xml tree
set oRootElement = objXMLDoc.documentElement
'I set the first element after root that is <ElencoElementi>
'under whom I have to insert <Elemento>
set oNodeElencoElementi = oRootElement.ChildNodes.Item(0)
set oNodeElemento = objXMLDoc.createNode(1,"Elemento","")
oNodeElencoElementi.appendChild oNodeElemento
set oNodeElementoId = objXMLDoc.createNode(1,"IDElemento","")
oNodeElemento.appendChild oNodeElementoId
set oNodeStrId = objXMLDoc.createNode(1,"string","")
oNodeStrId.Text = ID_ToAdd
oNodeElementoId.appendChild oNodeStrId
set oNodeElementoVal = objXMLDoc.createNode(1,"ValoreElemento","")
oNodeElemento.appendChild oNodeElementoVal
set oNodeStrVal = objXMLDoc.createNode(1,"string","")
oNodeStrVal.Text = ValueToAdd
oNodeElementoVal.appendChild oNodeStrVal
objXMLDoc.Save(myVar)
set oNodeStrVal = Nothing
set oNOdeStrId = Nothing
set oNodeElementoVal = Nothing
set oNodeElementoId = Nothing
set oNodeElemento = Nothing
set oNodeElencoElementi = Nothing
set oRootElement = Nothing
Set objXMLDoc = Nothing
'************************************************
Il risultato sarà questo:
Let's see now how to read from an XML file. Suppose we want to read from the above file all "IDElemento".
To do this is possible to use the getElementsByTagName method of the oRootElement object. We'll do a cycle for all oRootElement.ChildNodes and show the "IDElemento" Value.
'*************************************************
'********************* M A I N ********************
'*************************************************
Dim objXMLDoc, oRootElement, oNodeElementoId, i
Const myVar = "c:\temp\element.xml"
'MS XML DOM
set objXMLDoc=CreateObject("Microsoft.XMLDOM")
objXMLDoc.async="false"
'loading xml file ---> myVar
objXMLDoc.load(myVar)
'the root element of the xml tree
set oRootElement = objXMLDoc.documentElement
set oNodeElementoID = oRootElement.getElementsByTagName("IDElemento")
for i = 0 to oRootElement.childNodes.Length
msgbox oNodeElementoID(i).childNodes(0).Text
next
set oNodeElementoID = Nothing
set oRootElement = Nothing
'*************************************************
_______________________________________________________________________
Questo sito è stato realizzato con Jimdo! Registra il tuo sito gratis su https://it.jimdo.com