From: Pandu on 21 Sep 2010 14:32 I am using the code below to parse thru several XML files and print out data from each XML file. It only looks for files that starts with BEX and that are older than 2 days. However the code does not run if there are more than one file to parse. At that point I get the error -- VBSCript runtime error: Object required:'ElemList.item(...)' But it runs fine if there is only one file to parse. I think the error is caused by the array not incrementing (see "server = ElemList.item(0).Text" in the code below ). But I am not really sure - would appreciate any advice. TIA CODE ---> Dim server, filepath Dim fso,fold,fil,XMLDoc Set fso = CreateObject("Scripting.FileSystemObject") Set fold = fso.GetFolder("C:\Program Files\Symantec\Backup Exec \Data") For each fil in fold.files if (DateDiff("d",Now,fil.DateCreated) < 0) And (UCase(Left((fil.Name),3)) = "BEX") Then Set xmlDoc = CreateObject("Msxml2.DOMDocument") fil = fold & "\" & fil.name wscript.echo fil xmlDoc.load(fil) Set ElemList = xmlDoc.getElementsByTagName("jobServer") server = ElemList.item(0).Text Wscript.Echo server Set ElemList = xmlDoc.getElementsByTagName("timeStart") start_time = Replace(ElemList.item(0).Text,"Job started:","") Wscript.Echo start_time Set ElemList = xmlDoc.getElementsByTagName("timeEnd") end_time = Replace(ElemList.item(0).Text,"Job ended:","") Wscript.Echo end_time Set ElemList = xmlDoc.getElementsByTagName("completeStatus") engine_completion_status = Replace(ElemList.item(0).Text,"Job completion status:","") Wscript.Echo engine_completion_status End if Next <--- CODE
From: Pegasus [MVP] on 21 Sep 2010 16:47 "Pandu" <rsarcar(a)gmail.com> wrote in message news:b0883191-5253-4337-816f-cbc744d1aa31(a)m1g2000vbh.googlegroups.com... > I am using the code below to parse thru several XML files and print > out data from each XML file. It only looks for files that starts with > BEX and that are older than 2 days. However the code does not run if > there are more than one file to parse. At that point I get the error > -- VBSCript runtime error: Object required:'ElemList.item(...)' > > But it runs fine if there is only one file to parse. I think the error > is caused by the array not incrementing (see "server = > ElemList.item(0).Text" in the code below ). > > But I am not really sure - would appreciate any advice. > > TIA > > CODE ---> > > Dim server, filepath > Dim fso,fold,fil,XMLDoc > > Set fso = CreateObject("Scripting.FileSystemObject") > Set fold = fso.GetFolder("C:\Program Files\Symantec\Backup Exec > \Data") > > For each fil in fold.files > if (DateDiff("d",Now,fil.DateCreated) < 0) And > (UCase(Left((fil.Name),3)) = "BEX") Then > Set xmlDoc = CreateObject("Msxml2.DOMDocument") > > fil = fold & "\" & fil.name > wscript.echo fil > xmlDoc.load(fil) > > Set ElemList = xmlDoc.getElementsByTagName("jobServer") > server = ElemList.item(0).Text > Wscript.Echo server > > > Set ElemList = xmlDoc.getElementsByTagName("timeStart") > start_time = Replace(ElemList.item(0).Text,"Job > started:","") > Wscript.Echo start_time > > > Set ElemList = xmlDoc.getElementsByTagName("timeEnd") > end_time = Replace(ElemList.item(0).Text,"Job > ended:","") > Wscript.Echo end_time > > Set ElemList = > xmlDoc.getElementsByTagName("completeStatus") > engine_completion_status = > Replace(ElemList.item(0).Text,"Job completion status:","") > Wscript.Echo engine_completion_status > > End if > Next > > <--- CODE Have you tried inserting the line Set xmlDoc = nothing just before the "End if" statement? Also, to make it easier for respondents to help you, how about quoting the line that generates the error you see?
From: ekkehard.horner on 21 Sep 2010 16:58 Pandu schrieb: > I am using the code below to parse thru several XML files and print > out data from each XML file. It only looks for files that starts with > BEX and that are older than 2 days. However the code does not run if > there are more than one file to parse. At that point I get the error > -- VBSCript runtime error: Object required:'ElemList.item(...)' > > But it runs fine if there is only one file to parse. I think the error > is caused by the array not incrementing (see "server = > ElemList.item(0).Text" in the code below ). > > But I am not really sure - would appreciate any advice. > > TIA > > CODE ---> > > Dim server, filepath > Dim fso,fold,fil,XMLDoc > > Set fso = CreateObject("Scripting.FileSystemObject") > Set fold = fso.GetFolder("C:\Program Files\Symantec\Backup Exec > \Data") > > For each fil in fold.files > if (DateDiff("d",Now,fil.DateCreated)< 0) And > (UCase(Left((fil.Name),3)) = "BEX") Then > Set xmlDoc = CreateObject("Msxml2.DOMDocument") > > fil = fold& "\"& fil.name > wscript.echo fil > xmlDoc.load(fil) > > Set ElemList = xmlDoc.getElementsByTagName("jobServer") > server = ElemList.item(0).Text > Wscript.Echo server > > > Set ElemList = xmlDoc.getElementsByTagName("timeStart") > start_time = Replace(ElemList.item(0).Text,"Job > started:","") > Wscript.Echo start_time > > > Set ElemList = xmlDoc.getElementsByTagName("timeEnd") > end_time = Replace(ElemList.item(0).Text,"Job > ended:","") > Wscript.Echo end_time > > Set ElemList = > xmlDoc.getElementsByTagName("completeStatus") > engine_completion_status = > Replace(ElemList.item(0).Text,"Job completion status:","") > Wscript.Echo engine_completion_status > > End if > Next > > <--- CODE getElementsByTagName() will return a fresh (possibly empty) collection of elements for each call. Incrementing the index makes no sense at all. Either the info you want to get are contained in the first item of that collection or not. I'm fairly sure that the error is caused by your second file not containing the tag(s) you are asking for. See: Dim sFSpec : sFSpec = resolvePath( "..\testdata\xml\textonly.xml" ) Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument" ) oXDoc.async = False oXDoc.load sFSpec If 0 = oXDoc.ParseError Then WScript.Echo sFSpec, "looks ok" Dim sTag For Each sTag In Array( "digit", "notext", "Nixda" ) Dim ndlFnd : Set ndlFnd = oXDoc.getElementsByTagName( sTag ) WScript.Echo sTag, TypeName( ndlFnd ), ndlFnd.length, "elms" Dim sText On Error Resume Next sText = ndlFnd.Item(0).Text If 0 <> Err.Number Then WScript.Echo Err.Description Else WScript.Echo "Text:", sText End If On Error GoTo 0 Next Else WScript.Echo oXDoc.ParseError.Reason End If applied to <?xml version="1.0" encoding="iso-8859-1"?> <root> <code> <![CDATA[ If number <> 1234 Then WScript.Echo "wrong number" ]]> </code> <number> <digit>1</digit><digit>2</digit> <digit>3</digit><digit>4</digit> </number> <notext/> </root> output: M:\lib\kurs0705\testdata\xml\textonly.xml looks ok digit IXMLDOMSelection 4 elms Text: 1 notext IXMLDOMSelection 1 elms Text: Nixda IXMLDOMSelection 0 elms Object required
|
Pages: 1 Prev: adding event controls to a dynamic table Next: Windows 7 login vbscript not working |