简体   繁体   中英

how to validate XML node text using vbscript

This is my XML file.

<PARAMETER>
 <ETPAR_GUIX>
    <item>
       <PNAME>COAS_1</PNAME>
       <PTYP>X</PTYP>
       <PDESC>Generated Table for View</PDESC>
       <PINDEX>0001</PINDEX>
       <PGROUP>GETTAB</PGROUP>
       <XMLREF_TYP>T</XMLREF_TYP>
       <PSTRUC_TYP>T</PSTRUC_TYP>
       <PREF_TYPE>VIEW</PREF_TYPE>
       <PREF_NAME>COAS</PREF_NAME>
       <PDATLEN>0000</PDATLEN>
       <PINTLEN>000000</PINTLEN>
       <PDECIMALS>000000</PDECIMALS>
       <SORT_LNR>0001</SORT_LNR>
       <PREF_NAME2>COAS</PREF_NAME2>
       <VAL_TYPE>T</VAL_TYPE>
       <TAB_INDEX>0</TAB_INDEX>
    </item>
    <item>
       <PNAME>I_ORDER_NUMBER_FROM_TABLE</PNAME>
       <PTYP>I</PTYP>
       <PDESC>Generated Table for View</PDESC>
       <PINDEX>0001</PINDEX>
       <PGROUP>I.01</PGROUP>
       <XMLREF_TYP>T</XMLREF_TYP>
       <PSTRUC_TYP>T</PSTRUC_TYP>
       <PREF_TYPE>VIEW</PREF_TYPE>
       <PREF_NAME>COAS</PREF_NAME>
       <PDATLEN>0000</PDATLEN>
       <PINTLEN>000000</PINTLEN>
       <PDECIMALS>000000</PDECIMALS>
       <SORT_LNR>0001</SORT_LNR>
       <PREF_NAME2>COAS</PREF_NAME2>
       <VAL_TYPE>T</VAL_TYPE>
       <TAB_INDEX>0</TAB_INDEX>
    </item>
  </ETPAR_GUIX>
</PARAMETER>

I want to check whether XML node <PNAME> is starting with letter "I" or not. If all XML node <PNAME> is not starting with letter "I" then VB script should display error. This is my tried vb script :

Const XMLDataFile = "D:\Automation\imp\p.xml"

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)

xmlDoc.validateOnParse = True

If xmlDoc.Load(XMLDataFile) Then
  msgbox "SUCCESS loading XML File"
Else  
  msgbox "ERROR loading XML File"
End If

counter=0
Set root = xmlDoc.documentElement
Set items = root.childNodes

for each item in items
  myPNAME = xmlDoc.getElementsByTagName("PNAME").item(counter).text

  If (Left(myPNAME, 1) = "I") Then
    IsValid = True
  else
    IsValid=false
  End If
next

This above VB script code check every single <PNAME> letter si starting with "I" or not. But If none of the XML node <PNAME> letter is not starting with letter "I" then VB script should display error. Please help me. Thank You in advance.

You need to pre-set IsValid to False outside the loop and change the value to True if you find the first match. Change this:

for each item in items
  myPNAME = xmlDoc.getElementsByTagName("PNAME").item(counter).text

  If (Left(myPNAME, 1) = "I") Then
    IsValid = True
  else
    IsValid=false
  End If
next

into this:

IsValid = False
For Each item In items
  myPNAME = xmlDoc.getElementsByTagName("PNAME").item(counter).text

  If Left(myPNAME, 1) <> "I" Then
    IsValid = True
    Exit For
  End If
Next

'At this point the value of IsValid is True if at least one node text started
'with a capital I. Otherwise its value is False.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM