Announcement

Collapse
No announcement yet.

Parsing XML - getting error

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

    Parsing XML - getting error

    I'm trying to parse this XML (comes from URL). I used an example since I've never done this before, I cant find what is causing the problem. Much appreciate your help, thanks!

    Error I'm getting
    VB.Net script exception(0), re-starting: Object reference not set to an instance of an object.

    XML from URL...
    Code:
    fanspd<fanspd>0</fanspd>
    doorinprocess<doorinprocess>0</doorinprocess>
    timeremaining<timeremaining>0</timeremaining>
    macaddr<macaddr>60:CB:FB:99:99:0A</macaddr>
    ipaddr<ipaddr>192.168.0.20</ipaddr>
    model<model>2.5eWHF</model>
    softver: <softver>2.14.1</softver>
    interlock1:<interlock1>0</interlock1>
    interlock2: <interlock2>0</interlock2>
    cfm: <cfm>0</cfm>
    power: <power>0</power>
    inside:<house_temp>72</house_temp>
    <DNS1>192.168.0.1</DNS1>
    attic: <attic_temp>92</attic_temp>
    OA: <oa_temp>81</oa_temp>
    server response: <server_response>Posted
    OK<br/></server_response>
    DIP Switches: <DIPS>00000</DIPS>
    Remote Switch:<switch2>1111</switch2>
    Setpoint:<Setpoint>0</Setpoint>
    I need to pull out these items...
    fanspd
    cfm:
    attic:


    Code:
    Sub Main()
    
    Dim xmlDoc As New XmlDocument()
    Dim strdata as String
    Dim FanIP = "192.168.2.143"    
    Dim XML_Feed as integer = 0
    Dim strpath = "http://" & FanIP & "/fanspd.cgi"
    
    Try
    'Parse XML Data First Layer        
            xmlDoc.Load(strpath)
            Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
            For Each node As XmlNode In nodes
                strdata = node.SelectSingleNode("fanspd").InnerText
                If strdata <> "" Then 
                       hs.writelog("Airscape", "Fan Speed: " & strdata)  
                    Else
                       hs.writelog("AirScape", "Nothing found.")  
                End If
            Next
      
        'hs.SetDeviceString(1208, strdata, True)
        Catch ex As Exception
    hs.WriteLog ("AirScape", "Error: " & ex.Message)
    End Try
    End Sub
    Last edited by Ltek; July 4, 2017, 12:44 PM.

    #2
    so you want to scrape the data ?

    Why dont use jon00 data scraper instead? its really easy and it writes to a virtual device.


    click here
    Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
    Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




    HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

    Comment


      #3
      Originally posted by Malosa View Post
      so you want to scrape the data ?

      Why dont use jon00 data scraper instead? its really easy and it writes to a virtual device.


      click here
      I will need to data for the logic in the larger script I'm building. Since there is not much to pull on this (only 4 fields) it makes it quicker/more efficient to pull the data, use it, then write to virtual devices (if needed).

      Its nicer (for me and for others to leverage) to use a stand-alone script to do all the whole house fan functionality. I'm not a fan of using 3 or 4 different plugins/scripts to accomplish 1 goal. Avoids a Frankenstein.

      thanks for helping!

      Comment


        #4
        The sample data posted in your code tags appears to be malformed so it's a bit difficult to establish where the fault is - I would imagine it is likely to be in the XPath so it'd be worth seeing this. Try saving as an xml file and IIRC the board accepts XML files as attachments.

        You could also look at the .SelectSingleNode which might be more useful if the XML is basic and does not contain lists of nodes.

        Comment


          #5
          Originally posted by mrhappy View Post
          The sample data posted in your code tags appears to be malformed so it's a bit difficult to establish where the fault is - I would imagine it is likely to be in the XPath so it'd be worth seeing this. Try saving as an xml file and IIRC the board accepts XML files as attachments.

          You could also look at the .SelectSingleNode which might be more useful if the XML is basic and does not contain lists of nodes.
          Let me know if the below info helps... I'm not familiar with any of the XML methods - they confuse me, I don't get the logic of all the different ways XML can be formatted.

          thx for the help!

          When I simply type in the URL, here's what appears on in the browser...



          What the API docs show...

          Code:
          fanspd<fanspd>0</fanspd>
          doorinprocess<doorinprocess>0</doorinprocess>
          timeremaining<timeremaining>0</timeremaining>
          macaddr<macaddr>60:CB:FB:99:99:0A</macaddr>
          ipaddr<ipaddr>192.168.0.20</ipaddr>
          model<model>2.5eWHF</model>
          softver: <softver>2.14.1</softver>
          interlock1:<interlock1>0</interlock1>
          interlock2: <interlock2>0</interlock2>
          cfm: <cfm>0</cfm>
          power: <power>0</power>
          inside:<house_temp>72</house_temp>
          <DNS1>192.168.0.1</DNS1>
          attic: <attic_temp>92</attic_temp>
          OA: <oa_temp>81</oa_temp>
          server response: <server_response>Posted
          OK<br/></server_response>
          DIP Switches: <DIPS>00000</DIPS>
          Remote Switch:<switch2>1111</switch2>
          Setpoint:<Setpoint>0</Setpoint>
          Last edited by Ltek; July 4, 2017, 12:57 PM.

          Comment


            #6
            There is still the root node missing but I'm guessing it is called 'feed' on the back of your first example. Does this work and put the value in the log?

            Code:
            Try
            'Parse XML Data First Layer        
                    xmlDoc.Load(strpath)
            
            hs.writelog("", xmlDoc.SelectSingleNode("/feed/fanspd").InnerText)
              
                'hs.SetDeviceString(1208, strdata, True)
                Catch ex As Exception
            hs.WriteLog ("AirScape", "Error: " & ex.Message)
            End Try
            End Sub

            Comment


              #7
              Originally posted by mrhappy View Post
              There is still the root node missing but I'm guessing it is called 'feed' on the back of your first example. Does this work and put the value in the log?

              Code:
              Try
              'Parse XML Data First Layer        
                      xmlDoc.Load(strpath)
              
              hs.writelog("", xmlDoc.SelectSingleNode("/feed/fanspd").InnerText)
                
                  'hs.SetDeviceString(1208, strdata, True)
                  Catch ex As Exception
              hs.WriteLog ("AirScape", "Error: " & ex.Message)
              End Try
              End Sub
              Looks like there is NO root node.

              I used your code...

              Error: Data at the root level is invalid. Line 1, position 1.

              .. also tried //fanspd ... same prob

              Comment


                #8
                Well if there is no root node then it can't be well formed XML and any of the .Net methods to look through XML are likely to fail with similar exceptions. That is unless you wanted to try and add the root nodes yourself, not sure I've ever tried it but you could try

                Code:
                xmlDoc.LoadXML("<feed>" & strpath & "</feed>")
                but it is solving what appears to be a problem with the unit - is it an advertised API?

                You would need to switch from .Load to .LoadXML and read in the data using hs.geturl as .LoadXML wants all of the data as a string rather than going to a URL for you.

                Comment


                  #9
                  Yes, the API is 'advertised' in a Blog on the company web site... 3 years ago - hasnt changed, I talked to them a month ago.

                  Making progress... I tried some code (below) but not sure it is correct can you look it over to ensure my syntax, dims, etc are right?

                  I'm now gettting this error...
                  Jul-04 1:13:00 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\AirscapeUpdate.vb: Expression does not produce a value.
                  Jul-04 1:13:00 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\AirscapeUpdate.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

                  Code:
                  Sub Main(ByVal Parms As String)
                  Dim xmlDoc As New XmlDocument()
                  Dim strdata as String
                  Dim PageXML as String
                  Dim FanIP = "192.168.2.143"
                  Dim XML_Feed as integer = 0
                  Dim url = "http://" & FanIP
                  Dim page = hs.GetURL(url,"/fanspd.cgi",TRUE,80)
                  hs.WriteLog ("AirScape", page)
                  Try
                  'Parse XML Data First Layer
                          PageXML = xmlDoc.LoadXML("<feed>" & page & "</feed>")
                          hs.WriteLog ("AirScape", page)
                          Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
                          For Each node As XmlNode In nodes
                              strdata = node.SelectSingleNode("fanspd").InnerText
                              If strdata <> "" Then
                                     hs.writelog("Airscape", "Fan Speed: " & strdata)
                                  Else
                                     hs.writelog("AirScape", "Nothing found.")
                              End If
                          Next
                      'hs.SetDeviceString(1208, strdata, True)
                      Catch ex As Exception
                  hs.WriteLog ("AirScape", "Error: " & ex.Message)
                  End Try
                  End Sub

                  Comment


                    #10
                    I added some more logging... it definitely is pulling in the page...

                    but now I'm getting this error...

                    Error: '', hexadecimal value 0x18, is an invalid character. Line 16, position 46.

                    using this code...

                    Code:
                    Sub Main(ByVal Parms As String)
                    Dim xmlDoc As New XmlDocument()
                    Dim strdata as String
                    Dim PageXML as String
                    Dim FanIP = "192.168.2.143"
                    Dim XML_Feed as integer = 0
                    Dim url = "http://" & FanIP
                    Dim page = hs.GetURL(url,"/fanspd.cgi",FALSE,80)
                    hs.WriteLog ("AirScape", page)
                    Try
                    'Parse XML Data First Layer
                    hs.WriteLog ("AirScape", page)
                            xmlDoc.LoadXML("<feed>" & page & "</feed>")
                            Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
                            For Each node As XmlNode In nodes
                            hs.WriteLog ("AirScape", node)
                                strdata = node.SelectSingleNode("fanspd").InnerText
                                If strdata <> "" Then
                                       hs.writelog("Airscape", "Fan Speed: " & strdata)
                                    Else
                                       hs.writelog("AirScape", "Nothing found.")
                                End If
                            Next
                        'hs.SetDeviceString(1208, strdata, True)
                        Catch ex As Exception
                    hs.WriteLog ("AirScape", "Error: " & ex.Message)
                    End Try
                    End Sub

                    Comment


                      #11
                      Hang on - I thought in the example returns the data may have been prefixed ("fanspd: " then the XML attribute) for my ease to explain the bits, looking at the post in #5 it appears that this actually comes back in the data. That is most definitely not XML - it can't be, there is no root node and AFAIK you don't have data outside of tags in this way. It has some similarities - not sure if it is being used for some sort of HTML processing?

                      Could do it with string manipulation instead;

                      Code:
                                  Console.WriteLine(strdata)
                      
                                  Dim NumberOfChars As Byte = (InStr(strdata, "</fanspd>") - 1) - (InStr(strdata, "<fanspd>") + 7)
                      
                                  Console.WriteLine(NumberOfChars)
                      
                                  Console.WriteLine(strdata.Substring(InStr(strdata, "<fanspd>") + 7, NumberOfChars))
                      Swap the console.writeline for your hs.writelogs, not an ideal solution but quick!

                      Comment


                        #12
                        I was just about to ask if it would be better using RegEx. Wonder if there is an easy way to say 'grab all characters between'?

                        Your InStr example works - thank you.

                        Can you explain how NumberOfChars portion works?

                        I think I get some of it... start count 1 character before </fanspd> : but why +7 to <fanspd>?

                        thx

                        Comment


                          #13
                          Instr of < fanspd > will return the first occurrence in the string, it will return the opening angle bracket as a number so you have to add the number of characters after it to get where the data should be.

                          If you know how to use RegEx that is potentially going to be a better solution, I deem it as some type of sorcery that I don't understand.

                          Comment


                            #14
                            mrhappy - thank you for your help. Its working now and I'm setting virtual devices.

                            have a (mr)happy 4th!

                            Comment


                              #15
                              Originally posted by Ltek View Post
                              mrhappy - thank you for your help. Its working now and I'm setting virtual devices.

                              have a (mr)happy 4th!
                              Not a problem - just keep an eye on it and make sure the data does not change which could throw the values out.

                              I'll try and celebrate but I'm not sure we celebrate losing a large part of the empire all that much, it was all downhill from there until 1997

                              Comment

                              Working...
                              X