How to Manipulate XML with XPath in VBScript
I have recently started working with the XPath. I know that XPath is a syntax to designate a precise portion of an XML file. I am trying to read an existing XML file and extract the information needed but not succeeding in doing it. I am using the VBScript to to Manipulate XML. So please explain me how to Manipulate XML with XPath in VBScript??
Re: How to Manipulate XML with XPath in VBScript
You should use the DOM API from Microsoft ( Microsoft DOM ) for this. It is an implementation of the recommendation defined by the W3C that allows access to the contents of a document and edit it. The DOM is mainly used to edit XML documents or for accessing the content of web pages. In the course of the DOM document, we will use XPath. XPath is a syntax to designate a precise portion of an XML file.
Just assume the following XML document on which we are working:
Code:
<? xml version="1.0" encoding="ISO-8859-1"? > < personnes > < personne age = " 49 " > < nom > Baud < / nom > < prenom > Georges < / prenom > < etat > Marié < / etat > < enfants > < enfant > < nom > Tiop < / nom > < prenom > Elisabeth < / prenom > < / enfant > < / enfants > < / personne > < personne age = " 22 " > < nom > Trinzka < / nom > < prenom > Judith < / prenom > < etat > Célibataire < / etat > < / personne > < personne age = " 88 " > < nom > Godoh < / nom > < prenom > Madeleine < / prenom > < etat > Veuve < / etat > < enfants > < enfant > < nom > Godoh < / nom > < prenom > Jean-Marie < / prenom > < / enfant > < enfant > < nom > Godoh < / nom > < prenom > Etienne < / prenom > < / enfant > < enfant > < nom > Swoti < / nom > < prenom > Julienne < / prenom > < / enfant > < / enfants > < / personne > < / personnes >
So it is simply a list of people. Each person has an age, a state, a name and a surname and may possibly have kids.
We will now proceed to read the XML file. To begin, we must use a parser. For this, we will use the parser XMLDOM Microsoft.
The first thing to do in our code is to initialize a parser:
Code:
Set xmlDoc = CreateObject ( " Microsoft.XMLDOM " )
And at the end of our script, do not forget to destroy our object: Set Now we must open our file. It will also tell the parser to load the entire file into memory before starting to parse:
Code:
Set xmlDoc = CreateObject ( " Microsoft.XMLDOM " ) xmlDoc . Async = " false " xmlDoc . Load ( " personnes.xml " )
So we just passed the parser in synchronous mode to load the entire file into memory before processing it and then we open the file with the Load method. For example, we'll just create a small program that will read the XML document and view people in a dialog box. We will now retrieve all persons contained in the XML file. For this, we will use the method selectNodes which we pass an XPath query. The XPath query is simple, you will find all items in person tag people:
Code:
' On récupère tous les noeuds personnes ' Ã* l'intérieur d'un noeud personnes For Each personneElement In xmlDoc . selectNodes ( " /personnes/personne " ) Next
It has completed all the elements that people have been found in the root element. We will now retrieve the main information (name, state) and the display with the MsgBox method:
Code:
' On récupère les informations sur la personne nom = personneElement . selectSingleNode ( " nom " ) . text prenom = personneElement . selectSingleNode ( " prenom " ) . text etat = personneElement . selectSingleNode ( " etat " ) . text MsgBox " Nom : " & nom & vbcrlf & _ " Prénom : " & prenom & vbcrlf & _ " Etat civil : " & etat
We started by retrieving all child elements of the person and if any, they were driven to add and finally the display. If there is none, it simply displays an error message. There. So we managed to parse our XML file and extract the necessary information. The reading file is still on the same principle. Nodes are recovered, their children, their content and their attributes.
Re: How to Manipulate XML with XPath in VBScript
That was a great post mate. As you mentioned the tips for reading the XML, now you need to Write the XML. This is the form in which we have our data:
Code:
personnes = array () Redim personnes ( 1 ) georges = array () Redim georges ( 4 ) georges ( 0 ) = " Baud " georges ( 1 ) = " Georges " georges ( 2 ) = " Marié " georges ( 3 ) = 49 enfants = array () Redim enfants ( 0 ) elisabeth = array () Redim elisabeth ( 1 ) elisabeth ( 0 ) = " Tiop " elisabeth ( 1 ) = " Elisabeth " enfants ( 0 ) = elisabeth georges ( 4 ) = enfants personnes ( 0 ) = georges judith = array () Redim judith ( 4 ) judith ( 0 ) = " Trinzka " judith ( 1 ) = " Judith " judith ( 2 ) = " Célibataire " judith ( 3 ) = 22 judith ( 4 ) = array () personnes ( 1 ) = judith
And it will transform into a XML in the same form as the previous chapter we read. For writing, we will also need to initialize and configure our parser:
Code:
Set xmlDoc = CreateObject ( " Microsoft.XMLDOM " ) Set oCreation = xmlDoc . createProcessingInstruction ( " xml " , " version='1.0' encoding='ISO-8859-1' " ) xmlDoc . insertBefore oCreation, xmlDoc . childNodes . Item ( 0 )
The 2 lines after the initialization can generate the XML header. Then we will create the root and add to the document:
Code:
Set root = xmlDoc . createElement ( " personnes " ) xmlDoc . appendChild (root)
We therefore used the method createElement to create a new node and appendChild method to add the node to the document root. We will now go through our people to create an array element for each individual array elements:
Code:
For Each personne In personnes Set personneElement = xmlDoc . createElement ( " personne " ) root . appendChild (personneElement) Next
This time, they added items into the root node. We will now add key information about the person:
Code:
Set personneElement = xmlDoc . createElement ( " personne " ) Set nomElement = xmlDoc . createElement ( " nom " ) nomElement . Text = personne ( 0 ) personneElement . appendChild (nomElement) Set prenomElement = xmlDoc . createElement ( " prenom " ) prenomElement . Text = personne ( 1 ) personneElement . appendChild (prenomElement) Set etatElement = xmlDoc . createElement ( " etat " ) etatElement . Text = personne ( 2 ) personneElement . appendChild (etatElement) root . appendChild (personneElement)
If there are children, then we create a child element with the addition of child elements for each child of the person and finally we add a name and a surname for each child. Now save our file.
Re: How to Manipulate XML with XPath in VBScript
You must Indent the XML file. The method used is unfortunately much more complicated this time. Must be replaced by xmlDoc.Save:
Code:
set rdr = CreateObject ( " MSXML2.SAXXMLReader " ) set wrt = CreateObject ( " MSXML2.MXXMLWriter " ) Set oStream = CreateObject ( " ADODB.STREAM " ) oStream . Open oStream . Charset = " ISO-8859-1 " wrt . indent = True wrt . encoding = " ISO-8859-1 " wrt . output = oStream Set rdr . contentHandler = wrt Set rdr . errorHandler = wrt rdr . Parse xmlDoc wrt . flush oStream . SaveToFile " personnes2.xml " , 2 Set rdr = Nothing Set wrt = Nothing
What you need to do in this code is that it opens a SAX reader will parse our XML file and it gives the content of reading a MXXMLWriter which will allow us to write our own XML tags and indented Finally, it configures the stream out of the writer was told to write to the file of our choice. This gives you as output file:
Code:
< ?xml version = " 1.0 " encoding = " ISO-8859-1 " standalone = " no " ? > < personnes > < personne age = " 49 " > < nom > Baud < / nom > < prenom > Georges < / prenom > < etat > Marié < / etat > < enfants > < enfant > < nom > Tiop < / nom > < prenom > Elisabeth < / prenom > < / enfant > < / enfants > < / personne > < personne age = " 22 " > < nom > Trinzka < / nom > < prenom > Judith < / prenom > < etat > Célibataire < / etat > < / personne > < / personnes >
It is therefore directly much more readable.
Re: How to Manipulate XML with XPath in VBScript
Modify an existing XML file is not very complicated and it has already seen everything. He just have to read the XML file, modify the commands seen in the writing and then save it. For example, we will change the ages of people. We will add one year to each person in the XML. It will open in the same way as a normal play:
Code:
Set xmlDoc = CreateObject ( " Microsoft.XMLDOM " ) xmlDoc . Async = " false " xmlDoc . Load ( " personnes.xml " )
Then we will loop through all those and add a year:
Code:
' On récupère tous les noeuds personnes Ã* l'intérieur d'un noeud personnes For Each personneElement In xmlDoc . selectNodes ( " /personnes/personne " ) ' On récupère l'âge de la personne age = personneElement . getAttribute ( " age " ) ' On lui rajoute un an personneElement . setAttribute " age " , age + 1 Next
And finally, we will save our file.