Announcement

Collapse
No announcement yet.

Parse XML

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Parse XML

    I'm working on a script that can be used with SABNZBD.

    SABNZBD is a small web based program that is widely used on NAS servers and NMT's

    The program also produces a XML file with status info. (see Att.)

    Initially, (to get on the right track) I want to make 3 HS devices to store some info in.

    Let's say: paused = _50, kbpersec = _51 and noofslots = _52.

    How would i go about?

    This is what I think should be the start of the script, but do I read the nodes and put them in the devices. (the last part I understand)

    Code:
    sub main()
     
      Set oXML = CreateObject("Microsoft.XMLDOM")
      oXML.ValidateOnParse = False
      oXML.async="false"
      hs.setDeviceString virtualdevice, ""
      If (oXML.load("10.0.0.152:8081/sabnzbd/api?mode=qstatus&output=xml&apikey=550b956bf290d91ccb033382b3337acd")) then
    	processdata(oXML)		
      else
    	hs.writelog "SABNZBD+", "Failed to load XML file"
      end if
        
    end sub
    Regards,
    Attached Files

    #2
    If it were me, I'd start with the HS2 DVD Collection scripts and see what is useful for your XML file parsing. I'd say more, but really, that's exactly what I would do.
    huggy_d1

    Automating made easy

    Comment


      #3
      I have a script which parses XML (albeit for a different purpose), I am doing it like this;

      Code:
      Sub Main(data)
      set objelec = CreateObject("Microsoft.XMLDOM") 
      objelec.loadXML(data)
      Set objelec = objelec.selectSingleNode("msg/ch1/watts")
      hs.SetDeviceString "q1", objelec.text
      End Sub
      I would suggest (but have not tested) something like this to start you off - the load depends on where your XML is kept though, mine is passed into the script as raw data

      Code:
      Sub Main()
      set objtest = CreateObject("Microsoft.XMLDOM") 
      objtest.loadXML(app.path & "\[URL="http://forums.homeseer.com/attachment.php?attachmentid=21395&d=1252955297"]sabnzbd.xml[/URL]")
      Set objtest = objtest.selectSingleNode("queue/paused")
      hs.SetDeviceString "_50", objtest.text
      End Sub
      I have had a problem with multiple XML controls before in that I have to create multiple controls as I could not reuse the old ones, this has not presented a problem with me but can't imagine it is particularly efficient...

      Code:
      Sub Main()
      set objtest = CreateObject("Microsoft.XMLDOM") 
      objtest.loadXML(app.path & "\[URL="http://forums.homeseer.com/attachment.php?attachmentid=21395&d=1252955297"][COLOR=#800080]sabnzbd.xml[/COLOR][/URL]")
      Set objtest = objtest.selectSingleNode("queue/paused")
      hs.SetDeviceString "_50", objtest.text
      Set objtest = objtest.selectSingleNode("queue/[COLOR=#990000]kbpersec[/COLOR]")
      hs.SetDeviceString "_51", objtest.text
      Set objtest = objtest.selectSingleNode("queue/noofslots")
      hs.SetDeviceString "_52", objtest.text
      End Sub
      Try it and it might work...

      Comment


        #4
        When I use the following code:
        Code:
        sub main()
           
         set objtest = CreateObject("Microsoft.XMLDOM") 
         objtest.loadXML("10.0.0.152:8081/sabnzbd/api?mode=qstatus&output=xml&apikey=550b956bf290d91ccb033382b3337acd")
        
         Set objtest = objtest.selectSingleNode("queue/paused")
          hs.SetDeviceString "Q1", objtest.text
         'Set objtest = objtest.selectSingleNode("queue/kbpersec")
         ' hs.SetDeviceString "Q2", objtest.text
         'Set objtest = objtest.selectSingleNode("queue/noofslots")
         ' hs.SetDeviceString "Q3", objtest.text
        End Sub
        I get the following error in the HS log:

        Running script, script run or compile error in file: Sabnzbd.txt424:Object required: 'objtest' in line 7 More info: Object required: 'objtest'

        Comment


          #5
          1) take out the loadxml, see if the script runs without errors - in fact just create the XMLDOM and dont load/select anything
          2) what version of .net are you running?

          Comment


            #6
            Maybe something simple but, is this script saved as an .vb script?

            Comment


              #7
              I've done it as a text vbscript, have to admit i'm not that good with .net scripts...if all you are doing is parsing xml and putting in device strings probably no difference in either format. Should not be that different as a .net script just a few changes of syntax...try saving the above as a .txt see if it works..

              Comment


                #8
                That's what I initially did. So I will keep it with .txt

                When disableing loadxml, the same error comes up

                I'll try it with a saved xml file iso downloading.

                Comment


                  #9
                  Originally posted by glabbeek View Post
                  When I use the following code:
                  Code:
                  sub main()
                     
                   set objtest = CreateObject("Microsoft.XMLDOM") 
                   objtest.loadXML("10.0.0.152:8081/sabnzbd/api?mode=qstatus&output=xml&apikey=550b956bf290d91ccb033382b3337acd")
                  
                   Set nodetest = objtest.selectSingleNode("queue/paused")
                    hs.SetDeviceString "Q1", nodetest.text
                   'Set node1test = objtest.selectSingleNode("queue/kbpersec")
                   ' hs.SetDeviceString "Q2", node1test.text
                   'Set node2test = objtest.selectSingleNode("queue/noofslots")
                   ' hs.SetDeviceString "Q3", node2test.text
                  End Sub
                  I get the following error in the HS log:

                  Running script, script run or compile error in file: Sabnzbd.txt424:Object required: 'objtest' in line 7 More info: Object required: 'objtest'
                  The node object needs to be unique. If you look at the DVD collections scripts, you will see one that looks for an error when searching for a single node. Try that as it's also vbscript. VB.NET is a different solution for picking out nodes. I hear it's easier but not necessarily faster if you're not vb.net savvy (like me).
                  huggy_d1

                  Automating made easy

                  Comment


                    #10
                    Your right, i'm struggling aswell...really scratching my head on this one! It must be something obvious.

                    I've attached my script and the xml file I read, I can't see what different i'm doing and I run the script below every minute...it might help you
                    Last edited by mrhappy; July 22, 2010, 04:36 PM.

                    Comment


                      #11
                      Code:
                      sub main()
                      
                      	dim XMLfile
                      	XMLfile = hs.GetURL("10.0.0.152","/sabnzbd/api?mode=qstatus&output=xml&apikey=550b956bf290d91ccb033382b3337acd",FALSE,8081)
                      	msgbox XMLfile 'test to check if xml gets loaded
                      
                      	set oXML = CreateObject("Microsoft.XMLDOM")
                      	oXML.async = FALSE
                      	oXML.LoadXML(XMLfile)
                      	set test_value = oXML.selectsinglesnode("queue/paused")
                      	hs.SetDeviceString "q1", test_value.text
                      
                      end sub
                      No errors, but also no device change.

                      Comment


                        #12
                        Stuck again

                        I've got the parsing working now:

                        Code:
                        sub main()
                        	dim hl,start_hc,ip,apikey
                        '======================================================================================================
                        'Change only the values below
                        	
                        	hc="q"					'housecode for device to be made by script
                        	start_dc="1"			'lowest devicecode, so if you want to start with "q1"  this should be 1
                        	ip="10.0.0.152:8081"	'IP adres + portnumber SABNZBD (can also be localhost)
                        	apikey="550b956bf290d91ccb033382b3337acd" 'apikey can be found in config/general of SABNZBD
                        '======================================================================================================	
                        	
                        	strpath = "http://" & ip & "/sabnzbd/api?mode=qstatus&output=xml&apikey=" & apikey
                        	
                        	Set oXML = CreateObject("Microsoft.XMLDOM")
                        	oXML.async = False
                        	oXML.load(strpath)
                        	
                        	Set node_paused = oXML.selectSingleNode("queue/paused")
                        	hs.SetDeviceString "q1", node_paused.text
                        	Set node_kbpersec = oXML.selectSingleNode("queue/kbpersec")
                        	hs.SetDeviceString "q2", node_kbpersec.text
                        	Set node_mbleft = oXML.selectSingleNode("queue/mbleft")
                        	hs.SetDeviceString "q3", node_mbleft.text
                        	Set node_mb = oXML.selectSingleNode("queue/mb")
                        	hs.SetDeviceString "q4", node_mb.text
                        	Set node_noofslots = oXML.selectSingleNode("queue/noofslots")
                        	hs.SetDeviceString "q5", node_noofslots.text
                        	Set node_diskspace1 = oXML.selectSingleNode("queue/diskspace1")
                        	hs.SetDeviceString "q6", node_diskspace1.text
                        	Set node_diskspace2 = oXML.selectSingleNode("queue/diskspace2")
                        	hs.SetDeviceString "q7", node_diskspace2.text
                        	Set node_timeleft = oXML.selectSingleNode("queue/timeleft")
                        	hs.SetDeviceString "q8", node_timeleft.text
                        end sub
                        The problem is that script works when saved as *.txt (so it is a VBscript, I think)

                        I now want to ad some scripting that checks if the devices exists and if not will make new ones.

                        Code:
                        Sub Main (parms As Object) 
                        
                        '======================================================================================================
                        'Change only the values below
                        	
                        Dim	hc As String ="q"					'housecode for device to be made by script
                        Dim	start_dc As Integer ="1"			'lowest devicecode, so if you want to start with "q1"  this should be 1
                        Dim	ip As String ="10.0.0.152:8081"	'IP adres + portnumber SABNZBD (can also be localhost)
                        Dim	apikey As String ="550b956bf290d91ccb033382b3337acd" 'apikey can be found in config/general of SABNZBD
                        '======================================================================================================	
                        
                        Dim count As Long = 0 
                        Dim i As Integer = 17 					'Number of devices to be made
                        Dim UDevcode As String  
                        Dim DevExists As String 
                        Dim DV As Object 
                        Dim DevRef as object
                        
                        
                        while count < i
                        	UDevcode = hc & (Csng(start_dc + Count))
                        	DevExists = hs.DeviceExists(UDevcode) 
                        	If DevExists = -1 Then 
                        		DevRef = hs.NewDeviceRef("SABNZBd " & Cstr(count+1))
                        		DV = hs.GetDeviceByRef(DevRef) 
                        		DV.location = "SABNZBd" 
                        		DV.hc = left(UDevcode,1) 
                        		DV.dc = Mid(UDevcode,2)
                        		DV.misc = &h10 
                        		DV.dev_type_string = "Status Only" 
                        		DV = Nothing 
                        				 
                        	End If 
                        	count=count+1
                        end while
                        end sub
                        This, by itself, works as well. The problem is this is VB (.net I think) and won't work when placed with the first script.

                        How do I get these snippets together?

                        Comment


                          #13
                          Originally posted by huggy_d1 View Post
                          If it were me, I'd start with the HS2 DVD Collection scripts and see what is useful for your XML file parsing. I'd say more, but really, that's exactly what I would do.
                          I think this one might be easier...it was made by Jon00, and I modified it for the Audrey.

                          http://forums.homeseer.com/showthrea...rinks+database

                          Very easy to use, simple code.

                          --Dan
                          Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

                          Comment

                          Working...
                          X