Solved

Parsing Attributes Values in XML into an array

Posted on 2004-04-12
4
669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

623 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