Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find values within an XML Document

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601
I have done this on a few occasions and it is always a struggle. I just keep trying stuff until I get what I need but there has to be a technique that I can always apply to find things.

So what is the best way to find attributes for an element which might have 1 or might have multiple occurrences

For example <root><parentinfo><father attrib1="X" attrib2 = "y" /></parentinfo>
<children><child attrib1="x" attrib2 = "y" />
<child attrib1="a" attrib2 = "x" /><child attrib1="d" attrib2 = "e" /></children></root>



 
what do you mean?

First what are you using to access the XML?
XSL, DOM...?

either way, you can use XPath to access the Nodes...
Code:
<root>
  <parentinfo>
    <father attrib1="X"  attrib2 = "y" />
  </parentinfo>
  <children>
    <child attrib1="x" attrib2 = "y" />
    <child attrib1="a" attrib2 = "x" />
    <child attrib1="d" attrib2 = "e" />
  </children>
</root>

Method 1:
"//father[1]"
"//child[1]"
"//child[2]"
"//child[3]"

(I never can remember if these are zero based or not... if this does not work, use 0, 0, 1, 2 instead)

Method 2:
"//father"
"//child[attrib1 = 'x']"
"//child[attrib1 = 'a'][attrib2 = 'x']"
"//child[attrib1 = 'd'][attrib2 = 'e']"


If you are using DOM, use SelectSingleNode() with the above strings...

Hope This Helps...
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Try this code:
***************

Set nodeAttribute1 = xmla.selectSingleNode("//STATUS[0]")
Set objAttributes1 = nodeAttribute1.Attributes
Set objRefAttr1 = objAttributes1.getNamedItem("AttrName")

O = objRefAttr1.Text

****************************
In the code STATUS is the name of the element that has the
attribute whose value you are trying to get to. STATUS[0] means that we are looking for the first attribute of this element. You can adjust this value based on the number of attributes you have inside your element.
In the third line of code ("AttrName") is the name of the attribute whose value you are trying to get to.
 
if you want to check children the father node in xpath you can try these various ways:

Code:
<xsl:template match="father">
    <xsl:if test="child"> <!--father has children -->
     ...
    </xsl:if>

<!-- or to do something when the count of the children > x-->
   <xsl:if test="count(child) > 2">
    ...
    <xsl:if>

<!-- or print how many children father has -->

    <xsl:value-of select="count(child)"/>

</xsl:template>

hope that helps.


m


 
NonDrinker,
That looks like good information. How do you dimension those object variables you used?

nodeAttribute1
objAttributes1
objRefAttr1

To my way of looking your selectsinglenode is going to return a node (or maybe an elemeent?) - not an attribute?
 
You could leave them as variants... (especially in vbscript)
Dim nodeAttribute1, objAttributes1, objRefAttr1

Otherwise, it looks like:
Dim nodeAttribute1 As IXMLDOMNode
Dim objAttributes1 As IXMLDOMNodeList
Dim objRefAttr1 As IXMLDOMNode
Dim O As String

(to use these VB6 requires requires a reference to MSXML.DLL)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top