Solved

Parsing Attributes Values in XML into an array

Posted on 2004-04-12
4
654 Views
Last Modified: 2013-11-19
This is probably my 5th question and so far i learned a lots thru this portal.

Q) I have a xml file that i need to parse the attribute values. The sample file is

<Request>
      <PunchOutSetupRequest operation="create">
                  <BuyerCookie>34234234ADFSDF234234</BuyerCookie>
                  <Extrinsic name="FirstName">Sam</Extrinsic>
                  <Extrinsic name="LastName">Trusky</Extrinsic>
                  <Extrinsic name="Email">abc@mail.com</Extrinsic>
      </PunchOutSetupRequest>
</Request>

I need to retrieve the firstname, lastnamem and Email from the xml file.

Dim objPerson
Set objPerson = Server.CreateObject("Msxml2.DOMDocument")
objPerson .async = false
Response.Write "Ready to execute XML.load()"
objPerson.load(Server.MapPath("PunchOutOrderMessage.xml"))
objPerson.documentElement.firstChild
objPerson.getAttribute("FIRSTNAME")
....

I want to first store the values in a array since the list of attributes can vary.

Any help is appreciated.

0
Comment
Question by:CIT_DEV
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 10806243
There is no attribute named "FIRSTNAME" in your example.  Remember, everything is case sensitive, and you use the name of the attribute, not the value.  Perhaps you're trying to do this:

objPerson.childNodes.item(1).getAttribute("name").nodeValue

This returns "FirstName"

Or are you trying to get "Sam" as the returned value?  If so, you're looking for an <Extrinsic> node whose "name" attribute equals "FirstName".  SelectSingleNode and XPath is the easiest way to do this, but that doesn't work on all parsers.  What parser are you using?

Regards,
Mike Sharp
0
 
LVL 1

Author Comment

by:CIT_DEV
ID: 10806339
I am looking to retrieve the vaues of Sam and store it in a variable array item - FirstName

I am using Xpath.
0
 
LVL 1

Author Comment

by:CIT_DEV
ID: 10806702
This is the exact progam i am using..

Set objXML=Server.CreateObject("Microsoft.XMLDOM")
Dim aOrder(3,5)
If Request.QueryString("id") = 1 then
      objXML.load(Server.MapPath("Attributes.xml"))
End If

Response.Write "<textarea rows=20 cols=100  id=textarea1 name=textarea1>" & objXML.xml  & "</textarea>"
             
If objXML.parseError.errorCode <> 0 then
      Response.write "Error loading the resource file "
Else
    Path            =      "/Request/PunchOutSetupRequest/Extrinsic" & "/text()"
    Set nodes      =      objXML.selectNodes(path)
    For each x in nodes
      Response.write  x.xml
   Next
                  
End If
      
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 250 total points
ID: 10807130
Don't use Microsoft.XMLDOM.  That's the old parser.  Use Msxml2.DOMDocument as in your first post.

This:

    Path          =     "/Request/PunchOutSetupRequest/Extrinsic" & "/text()"

selects the all of the Extrinsic text node children...probably not what you want.  Try this:

    Path          =     "/Request/PunchOutSetupRequest/Extrinsic[@name = 'FirstName']"
    Set nodes     =     objXML.selectNodes(path)
    For each x in nodes
      Response.write  x.text
    Next
 
This selects all Extrinsic nodes whose name attribute equals "FirstName".

Regards,
Mike Sharp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now