Solved

Why parse an XML file selectNodes doesn't work?

Posted on 2009-03-31
3
312 Views
Last Modified: 2013-11-05
I'm using SelectNodes from XMLDocument (In access 2003) in order to read all of the Nodes contained in the file, but I'm having some problems to get the second and third nodes in a loop.

When using this vb code, I receive two records of the first node ("//body2")
And go on to the rest of the xml, how do I get the individual values?


The xml doc start like :  
 <?xml version="1.0" encoding="UTF-8" ?>
- <form1>
 .
 .
 .
- <section2>
  <header2 xmlns:xfa="http://www...../schema/xfa-data/1.0/" xfa:dataNode="dataGroup" />
- <body2>
  <contactPhone>07493153</contactPhone>
  <nameOfSchool>Ffff SCHOOL</nameOfSchool>
  <position>PRINCIPAL</position>
  <type>1</type>
  <givenNames>LEISA</givenNames>
  <surname>NEATON</surname>
  <employeeNo>2145</employeeNo>
  <fullName>LEISA NEATON</fullName>
  <location>School based</location>
  </body2>
- <body2>
  <contactPhone>07493153</contactPhone>
  <nameOfSchool>Fasdf</nameOfSchool>
  <position>TEACHER</position>
  <type>1</type>
  <givenNames>Barak</givenNames>
  <surname>OBAMA</surname>
  <employeeNo>2189</employeeNo>
  <fullName>Barak OBAMA</fullName>
  <location>School based</location>
  </body2>
  <addBody2 xmlns:xfa="http://www...../schema/xfa-data/1.0/" xfa:dataNode="dataGroup" />
  <Validate2 xmlns:xfa="http://www..../schema/xfa-data/1.0/" xfa:dataNode="dataGroup" />
  </section2>

.
..
...

 Dim nodeR As IXMLDOMNode

 

    For Each nodeR In xmlFile.selectNodes("//body2")

        rst.AddNew

        rst!fldAppID = AppID

        rst!fldsName = nodeR.selectSingleNode("//surname").Text

        rst!fldgNames = nodeR.selectSingleNode("//givenNames").Text

        rst!fldPosition = nodeR.selectSingleNode("//position").Text

        rst!fldEORC = nodeR.selectSingleNode("//type").Text

        rst!fldEmployeeID = nodeR.selectSingleNode("//employeeNo").Text

        rst!fldPh = nodeR.selectSingleNode("//contactPhone").Text

        rst!fldSchoolDepartm = nodeR.selectSingleNode("//nameOfSchool").Text

        rst.Update

    Next nodeR

Open in new window

0
Comment
Question by:smayshar
  • 2
3 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24039690
So, fldName is being filled from <surname>, and fldNames is filled from <givenNames>?
Are the remaining fields filled with an empty string, or is it falling over as nodeR.selectSingleNode("//position") is Nothing?
0
 

Author Comment

by:smayshar
ID: 24055793
I am not sure that I understand your qustion, the records in the rst after running the code lokks like this:

NEATON LEISA PRINCIPAL 1 2145 07493153 Ffff SCHOOL
NEATON LEISA PRINCIPAL 1 2145 07493153 Ffff SCHOOL

insdeat of :

NEATON LEISA PRINCIPAL 1 2145 07493153 Ffff SCHOOL
OBAMA  Barak  TEACHER  1 2189........

it goes over the first node 2 times and contintue for the rest of the xml why ???
0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24057895
Aha, now it makes sense. What's happening is that you are using // in the xpath query.
Do the following and it should work, as it's selecting only the named node in nodeR, not the whole document.

For more information on how xpath works, have a look at this site:
http://www.w3schools.com/xpath/xpath_syntax.asp
rst!fldsName = nodeR.selectSingleNode("surname").Text

Open in new window

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

929 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

14 Experts available now in Live!

Get 1:1 Help Now