Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14401
  • Last Modified:

Read XML File Using VB Script Only

Hi..All

 i have a  XML File Formate is

<Person>
<uid>501126729
<sn>Goyal</sn>
<givenname>Amit</givenname>
<initials></initials>
<uid>501126729</uid>
<mail>goyal.amit@aa.com</mail>
</uid>
<uid>500782504
<sn>Sakamuri</sn>
<givenname>Ramana</givenname>
<initials></initials>
<uid>500782504</uid>
<mail>ramana.sakamuri@aa.com</mail>
</uid>
</Person>

Now i want to fetch all data of <sn>,<givenname>,<uid>,<initials>,<mail> for both user or more user,  using simple VB Script only
i have done this in VB using XML DSO object but not able to do in VB Script using XML DOM

PLZ try to solve my problem, its very urgent for me.

Thanks
Amit G.


0
amit_cry
Asked:
amit_cry
  • 9
  • 7
  • 2
2 Solutions
 
anthonywjones66Commented:
Amit,

Are you saying can't use XML DOM or you don't know how to?

Assuming the latter:-

Dim oDOM
Set oDOM = CreateObject("MSXML.DOMDocument")
oDOM.async = false
oDOM.load "c:\temp\person.xml"

For each oUID in oDOM.documentElement.childNodes
   
sUID = oUID.selectSingleNode("text()")
sSN = oUID.selectSingleNode("sn")
sGivenName = oUID.selectSingleNode("givenname")

Next

If you can't use the msxml. What can you use?  Scripting.FileSystemObject?

Anthony.
0
 
amit_cryAuthor Commented:
ok Anthony thanks for ur response

Actully i am using same code.
But i am getting an error in this code when its fetching data. its showing a dialog box

line:
char:
Object required: 'oUID.selectSingleNode(...)'
code:800A01AB
Source: MicroSoft VB Script Runtime Error

is there is any way to handle the error in VB Script only not in VB or ASP.
plz try to solve my problem

Thanks
Amit
0
 
anthonywjones66Commented:
It's not a matter of handling the error it's a matter of why are you getting the error?

If you are using the code I posted this error should not be possible.

I'm assuming you have changed this line:-

oDOM.load "c:\temp\person.xml"

to load the file you have.  Can you post the actual code you are using?

Anthony.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
b1xml2Commented:
Dim oXml
' ASP VBScript Equivalent code commented out
' Set oXml = Server.CreateObject("Msxml2.DOMDocument")
' oXml.async = False
' oXml.load Server.MapPath("data.xml")

Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load("c:\data.xml")

Dim oNodeList, oNode
Set oNodeList = oXml.selectNodes("/Person/uid")
Dim id 'As Long
Dim surname 'As String
Dim givenNames 'As String
Dim initials 'As String
Dim mail 'As String
For Each oNode in oNodeList
      'accessing the data
      id = CLng(oNode.childNodes(0).nodeValue)
      surname = oNode.selectSingleNode("sn").Text
      givenNames = oNode.selectSingleNode("givenNames").Text
      initials = oNode.selectSingleNode("initials").Text
      mail = oNode.selectSingleNode("mail").Text
Next
0
 
amit_cryAuthor Commented:
Hi.... b1xml2


Your code is working fine but it gives error if any child node is not presant in any of its parent node

like if "giveNames" node is not present of it parent node then this line "node.selectSingleNode("givenNames").Text" give error.

how will i check for this condition?

Thanks for your response

Thanks
Amit G.
0
 
b1xml2Commented:
Dim oXml
' ASP VBScript Equivalent code commented out
' Set oXml = Server.CreateObject("Msxml2.DOMDocument")
' oXml.async = False
' oXml.load Server.MapPath("data.xml")

Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load("c:\data.xml")

Dim oNodeList, oNode
Set oNodeList = oXml.selectNodes("/Person/uid")
Dim id 'As Long
Dim surname 'As String
Dim givenNames 'As String
Dim initials 'As String
Dim mail 'As String
Dim oSurname, oGivenNames, oInitials, oMail
For Each oNode in oNodeList
     'accessing the data
     id = CLng(oNode.childNodes(0).nodeValue)
     
     Set oSurname = oNode.selectSingleNode("sn")
     If Not oSurname Is Nothing Then surname = oSurname.Text
     
     Set oGivenNames = oNode.selectSingleNode("givenNames")
     If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text
     
     Set oInitials = oNode.selectSingleNode("initials")
     If Not oInitials Is Nothing Then initials = oInitials.Text
     
     Set oMail = oNode.selectSingleNode("mail")
     If Not oMail Is Nothing Then mail = oMail.Text
Next
0
 
anthonywjones66Commented:
B1xml2,

If a node isn't present the code will use the value from the previous iteration of the loop.

Try this:-

Dim oXml
' ASP VBScript Equivalent code commented out
' Set oXml = Server.CreateObject("Msxml2.DOMDocument")
' oXml.async = False
' oXml.load Server.MapPath("data.xml")

Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load("c:\data.xml")

Dim oNodeList, oNode
Set oNodeList = oXml.selectNodes("/Person/uid")
Dim id 'As Long
Dim surname 'As String
Dim givenNames 'As String
Dim initials 'As String
Dim mail 'As String
Dim oSurname, oGivenNames, oInitials, oMail
For Each oNode in oNodeList
     'accessing the data
     id = CLng(oNode.childNodes(0).nodeValue)
     
     surname = ProtectedGet(oNode.selectSingleNode("sn"))
     givenNames = ProtectedGet(oNode.selectSingleNode("givenNames"))
     initials = ProtectedGet(oNode.selectSingleNode("initials"))
     mail = ProtectedGet(oNode.selectSingleNode("mail"))
   
Next

Function ProtectedGet(ByVal voNode)
  If not voNode Is Nothing Then ProtectedGet = oNode.Text
End Function

Anthony
0
 
anthonywjones66Commented:
oops

Function ProtectedGet(ByVal voNode)
  If not voNode Is Nothing Then ProtectedGet = voNode.Text
End Function

0
 
b1xml2Commented:
the change you propose is marginal.
0
 
b1xml2Commented:

Dim oXml
' ASP VBScript Equivalent code commented out
' Set oXml = Server.CreateObject("Msxml2.DOMDocument")
' oXml.async = False
' oXml.load Server.MapPath("data.xml")

Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load("c:\data.xml")

Dim oNodeList, oNode
Set oNodeList = oXml.selectNodes("/Person/uid")
Dim id 'As Long
Dim surname 'As String
Dim givenNames 'As String
Dim initials 'As String
Dim mail 'As String

For Each oNode in oNodeList
     'accessing the data
      id = CLng(oNode.childNodes(0).nodeValue)
      
      surname = GetValue(oNode,"surname")
      givenNames = GetValue(oNode,"givenNames")
      initials = GetValue(oNode,"initials")
      mail = GetValue(oNode,"mail")
Next

Function GetValue(ByVal parentNode,ByVal childName)
Dim child
Set child = parentNode.selectSingleNode(childName)
If Not child Is Nothing Then GetValue = child.Text
End Function
0
 
anthonywjones66Commented:
I'm not looking for points.  I'm not sure fixing this bug can be defined as marginal though.  :0

Anthony.

0
 
b1xml2Commented:
plus what was proposed was nothing new. We can write even better abstractions, encapsulate this in classes if this resides in ASP, ad nauseam.

However, you must be careful anthony. You shouldn't re-use expert's code if the functionality pro-offered is marginal. That is considered hijacking and some asker get confused and award points to the wrong person. It has happened. Typically, if that is to take place, one must clearly state that this is a refinement and not to award points.

So be forewarned, what you did can be construed as a violation of the guidelines.
0
 
b1xml2Commented:
>>
Set oSurname = oNode.selectSingleNode("sn")
     If Not oSurname Is Nothing Then surname = oSurname.Text
     
     Set oGivenNames = oNode.selectSingleNode("givenNames")
     If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text
     
     Set oInitials = oNode.selectSingleNode("initials")
     If Not oInitials Is Nothing Then initials = oInitials.Text
     
     Set oMail = oNode.selectSingleNode("mail")
     If Not oMail Is Nothing Then mail = oMail.Text
<<

Fixed the situation, plus it was posted be4 your code.
0
 
anthonywjones66Commented:
B1Xml2,

Point taken.  I will be sure to point out that I'm offering a refinement and not expecting to be granted the points in my future posts.

That said I was trying (somewhat ineptly) to point out to you a bug in the code you posted above.  I am assuming the the originator wants to do something useful with these variables with in loop after they have been aquired.

Now look at this code

     Set oGivenNames = oNode.selectSingleNode("givenNames")
     If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text

and ask the question 'If the node givenNames is not present what is the value of the variable givenNames?'.  The answer is 'It depends on what value it was given in a previous iteration of the loop' which is a bug.

My post was not just a refinement for the sake good code construction but was to fix this bug.  I admit it was poorly presented I'll try to remember in future what the rules are. :)

Anthony.
0
 
b1xml2Commented:
Not a problem. If you might recall, there was a post where I provided alternatives to your XPath but because it amounted to the same, I stated not that I would not accept the points.

Now as to this,
>>
Set oGivenNames = oNode.selectSingleNode("givenNames")
If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text
<<

This is valid code. Your assertion "The answer is 'It depends on what value it was given in a previous iteration of the loop' which is a bug" is maniefestly incorrect. We are looking for the child element of the oNode. It will always be be looking for the child node of givenNames of the current oNode, because it is inside the for each loop. The code is correct and exact. The keyword set resets the variable during each loop so how can it be a bug?

The technical reasoning you present is flawed and that is why I have stated your code you present is marginal in change.
0
 
b1xml2Commented:
After all, if you look at the code and remember that this is after crappy VBScript, we are only interested in getting the text of the various nodes and not the nodes themselves. The child nodes are to be discarded. Thus even the assertion made as to whether it was given in a pervious iteration of the loop cannot hold water for 2 reasons. This is why I sometimes shake my head when I see spurious claims:

1. The node gets set (reset) during EACH and EVERY iteration. It looks for the child node WITHIN the specific CONTEXT of the current oNode in the NodeList.
2. We are not even bothered with the node per se as we per asker's instructions are concerned with the textual content only.

A little knowledge is truly a dangerous weapon.
0
 
b1xml2Commented:
You do realise that the nodes get either the reference to the child node looked for or set to nothing???!!! Obviously from your posts, this fact has escaped you.
0
 
anthonywjones66Commented:
B1xml2

I'm somewhat new to this EE marlaky and am still learning all the niceties. I've a lot of respect for your comments and I'm trying to be a nice as I can.   However you are still missing the point I'm trying to make.  Let me give you an almost fuller answer including a dry run of the code.


This was the loop of your code I was 'fixing'.

Dim id 'As Long
Dim surname 'As String
Dim givenNames 'As String
Dim initials 'As String
Dim mail 'As String
Dim oSurname, oGivenNames, oInitials, oMail
For Each oNode in oNodeList
     'accessing the data
     id = CLng(oNode.childNodes(0).nodeValue)
     
     Set oSurname = oNode.selectSingleNode("sn")
     If Not oSurname Is Nothing Then surname = oSurname.Text
     
     Set oGivenNames = oNode.selectSingleNode("givenNames")
     If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text
     
     Set oInitials = oNode.selectSingleNode("initials")
     If Not oInitials Is Nothing Then initials = oInitials.Text
     
     Set oMail = oNode.selectSingleNode("mail")
     If Not oMail Is Nothing Then mail = oMail.Text

    'What happens here?  Something useful has to happen here using the variables surname, givenNames, initials and mail otherwise why have the loop at all?

Next

Now let's just invent something that uses the variables so we can see what happens, we'll insert this code into the bottom of the loop.


Response.Write "<tr>"
Response.Write "<td>" & Server.HTMLEncode(surname) & "</td>"
Response.Write "<td>" & Server.HTMLEncode(givenNames) & "</td>"
Response.Write "<td>" & Server.HTMLEncode(initials) & "</td>"
Response.Write "<td>" & Server.HTMLEncode(mail) & "</td>"
Response.Write "<tdr>"

Assume other sensible HTML construction is in place.

Now lets feed it this XML:-

<Person>
<uid>501126729
<sn>Goyal</sn>
<givenname>Amit</givenname>
<initials></initials>
<uid>501126729</uid>
<mail>goyal.amit@aa.com</mail>
</uid>
<uid>500782504
<sn>Sakamuri</sn>
<initials></initials>
<uid>500782504</uid>
<mail>ramana.sakamuri@aa.com</mail>
</uid>
</Person>

Notice the second uid element has no givenName child.

Now dry run that through your loop.  After the first iteration the variables will have these values:-

surname = "Goyal"
givenNames = "Amit"
initials = ""
mail = "goyal.amit@aa.com"

So the output is:-

<tr><td>Goyal</td><td>Amit</td><td></td>><td>goyal.amit@aa.com</td></tr>

Now in the second iteration the givenName element is missing.  So when the code reaches this line:-

    Set oGivenNames = oNode.selectSingleNode("givenNames")

The current object held in oGivenNames (assigned during the pervious iteration of the loop) is overwritten with Nothing.

Now we execute this line:-

     If Not oGivenNames Is Nothing Then givenNames = oGivenNames.Text

Currently the variable givenNames contains the value from the previous loop "Amit".  Since oGivenNames is Nothing the current value of givenNames is left as it is.

Hence at the end of this loop the variables have these values:-

surname = "Sakamuri"
givenNames = "Amit"
initials = ""
mail = "ramana.sakamuri@aa.com"

and the output will be:-

<tr><td>Sakamuri</td><td>Amit</td><td></td>><td>ramana.sakamuri@aa.com</td></tr>

do you see the bug now??

Anthony.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now