Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-04-2010
Member
 
Join Date: Jun 2009
Posts: 350
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??
Reply With Quote
  #2  
Old 18-04-2010
Member
 
Join Date: May 2008
Posts: 2,008
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
Code:
xmlDoc = Nothing
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.
Reply With Quote
  #3  
Old 18-04-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
  #4  
Old 18-04-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
  #5  
Old 18-04-2010
Member
 
Join Date: May 2008
Posts: 2,383
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Manipulate XML with XPath in VBScript"
Thread Thread Starter Forum Replies Last Post
How to manipulate an ISO images Zeppelin Operating Systems 4 31-12-2010 01:44 AM
What are the XForms and XPath? Nathen Software Development 5 03-03-2010 05:35 AM
What is XPath Eleeazar Software Development 3 21-11-2009 05:30 AM
Manipulate jQuery with PHP Zecho Software Development 5 16-06-2009 02:14 AM
Problem with inline elements in XPath Solitario Software Development 2 25-10-2008 05:27 PM


All times are GMT +5.5. The time now is 10:50 AM.