0

Can anyone give me a hint of how to get the values from G_Q9 if value in G_Q1 is equal to x? If value G_Q1 <> x then continue to the next G_Q1. I don't like the format of the XML file but I have to adapt to it.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Generated by Oracle Reports version 12.2.1.3.0 -->
<WOKE611>
  <LIST_G_Q1>
    <G_Q1>
      <Value>1234</Value>
      <LIST_G_Q9>
        <G_Q9>
          <Value>0</Value>
        </G_Q9>
        <G_Q9>
          <Value>1</Value>
        </G_Q9>
      </LIST_G_Q9>
    </G_Q1>
   <G_Q1>
      <Value>5678</Value>
      <LIST_G_Q9>
        <G_Q9>
          <Value>0</Value>
        </G_Q9>
        <G_Q9>
          <Value>1</Value>
        </G_Q9>
      </LIST_G_Q9>
    </G_Q1>
  </LIST_G_Q1>
</WOKE611>
Dim i

Set objXMLDoc = CreateObject("Msxml2.DOMDocument.6.0") 
objXMLDoc.Async = False 
objXMLDoc.Load("C:\temp\shortXml.xml")
Set NodeList = objXMLDoc.SelectNodes("//G_Q1/*")

For i = 1 To NodeList.Length 
    Set CurrNode = NodeList.NextNode
    If CurrNode.Text = "1234" Then
        WScript.Echo("How to continue to get the value of G_Q9/Value ?")
    End If
Next

I solved this by changing SelectNodes("//G_Q1/*") to SelectNodes("//*").

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
sova64
  • 1
  • 1
  • @Lankymart Not really a duplicate, at least not of that question, since contrary to the *answer* the OP edited in, the actual *question* is not about traversing the entire XML structure. – Ansgar Wiechers Mar 01 '19 at 11:13
  • @AnsgarWiechers *"I solved this by changing SelectNodes("//G_Q1/*") to SelectNodes("//*")."*. Regardless, it's a question about traversal of the XMLDOM (entire or not) which has been covered over and over again. – user692942 Mar 01 '19 at 11:17
  • 1
    Please re-read what I just wrote. – Ansgar Wiechers Mar 01 '19 at 11:18

1 Answers1

2

It's clearly not possible that the supposed solution you added to your question would actually solve the problem you described (at least not without further modifications to your code), since it would iterate over all XML nodes instead of selecting just the values you want.

A proper solution to the problem would look somewhat like this:

xpath = "//G_Q1[Value='1234']/LIST_G_Q9/G_Q9/Value"
Set NodeList = objXMLDoc.SelectNodes(xpath)
For Each n In NodeList
    WScript.Echo n.Text
Next

Replace WScript.Echo n.Text with whatever other processing you need to perform.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328