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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML & XPath - selectSingleNode method 1

Status
Not open for further replies.

bartekR

Technical User
Aug 18, 2007
24
0
0
GB
HI

I am having a problem with 'selectSingleNode' method.
It is not finding the node and i have no idea what am i doing wrong. It didn't work with my original code - i had a look on the net and found the below simple example from which i copied code and pasted xml content to my xml file but it still doesn't work!!!! I have set up a reference to MSXML v4.0 in tools.

Can anyone help ?
the bug must be somewhere in this line :

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='1' and @type='B']")


Please find below xml file and vba code.

xml file:
---------------------------------------------------------
<?xml version='1.0'?>
<EXAMPLE>
<CUSTOMER id="1" type="B">Mr. Jones</CUSTOMER>
<CUSTOMER id="2" type="C">Mr. Johnson</CUSTOMER>
</EXAMPLE>


code:
------------------------------------------------------------
Sub GetSingleValue()


Dim oXMLDoc As DOMDocument
Dim oXMLNode As IXMLDOMNode


Set oXMLDoc = New DOMDocument
oXMLDoc.LoadXML (Range("XML_Path").value)'checked it's ok

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='2' or @type='C']")
MsgBox oXMLNode.Text 'will show "Mr Johnson"

Set oXMLNode = oXMLDoc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='1' and @type='B']")


MsgBox oXMLNode.Text 'will show "Mr Jones"
MsgBox oXMLNode.Attributes.getNamedItem("id").Text 'will show "1"


End Sub


thank you
 
>oXMLDoc.LoadXML (Range("XML_Path").value)'checked it's ok
Then, it is _not_ ok.

[1] Either Range("XML_Path").value returns a path pointing to an xml document file. If that is the case, and that is what I guess the case, it is this.

[tt]oXMLDoc.[red]Load[/red] (Range("XML_Path").value)[/tt]

[2] or Range("XML_Path").value is a string serializing the xml document, ie, "<?xml..." kind of thing, then you have to echo it out for further inspection of well-formedness.

[3] In any case, you can put a slight control over the loading.
[tt]
dim bret as boolean
bret=oXMLDoc.Load (Range("XML_Path").value) 'or .LoadXML
[/tt]
bret must return true to warrant any further processing.
 
tsuji, thank you for taking time to respond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top