Solved

Parsing Attributes Values in XML into an array

Posted on 2004-04-12
4
656 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

17 Experts available now in Live!

Get 1:1 Help Now