Solved

Parsing Attributes Values in XML into an array

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use of cookies and regulations 1 86
XML Paring  Error - Premature end of file. 7 95
XML filtering Windows Event Viewer 10 55
Formatting issues in XSL FO 3 37
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

813 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