Announcement

Collapse
No announcement yet.

Parsing XML, Nothing Returning

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

    Parsing XML, Nothing Returning

    Ok what am I doing wrong. I have other scripts that parse XML just fine, but can't get this one to work. My main goal is to get to parse the weatherxml file that is local to my machine to just pull the Title for display on HSTouch. I have tried all other ways, and I always get the extra stuff that I don't need and makes it too much data.

    Here is the script:

    Code:
    Sub Main (parm as object)
    'Define XML Varibles
    Dim xmlDoc As New XmlDocument()
    Dim Alert_Title as String
    Dim XML_Feed as integer = 0
    Try
    'Parse XML Data First Layer        
            xmlDoc.Load("C:\Program Files\HomeSeer HS3\Data\weatherXML\NOAlerts1.xml")
            Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
            For Each node As XmlNode In nodes
                Alert_Title = node.SelectSingleNode("title").InnerText
            Next
        hs.writelog("Weather Alert", "Current Alert: " & Alert_Title)    
        hs.SetDeviceString(1208, Alert_Title, True)
        Catch ex As Exception
    hs.WriteLog ("Weather Alert Error", "Error: " & ex.Message)
    End Try
    End Sub
    It results in nothing being returned and no errors:

    Apr-14 8:51:25 AM Weather Alert Current Alert:
    Apr-14 8:51:25 AM
    Event Running script in background: C:/Program Files/HomeSeer HS3/scripts/Weather_Alert.vb
    Attached Files

    #2
    Can you attach your XML file and I will try it - it could be the XPath (I recommend XPathBuilder as a good tool for this) or the NameSpace (although I would expect an error that the NSManager was not found or something).

    Comment


      #3
      Be aware that XML and parsing XML is case sensitive so it could fail here

      Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
      (Also I think you need to remove the / here)

      and here

      Alert_Title = node.SelectSingleNode("title").InnerText

      Also try to move the write log line
      Code:
       Sub Main (parm as object)
      'Define XML Varibles
      Dim xmlDoc As New XmlDocument()
      Dim Alert_Title as String
      Dim XML_Feed as integer = 0
      Try
      'Parse XML Data First Layer        
              xmlDoc.Load("C:\Program Files\HomeSeer HS3\Data\weatherXML\NOAlerts1.xml")
              Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/feed")
              For Each node As XmlNode In nodes
                  Alert_Title = node.SelectSingleNode("title").InnerText
                  If Alert_Title <> "" Then 
                         hs.writelog("Weather Alert", "Current Alert: " & Alert_Title)  
                      Else
                         hs.writelog("Weather Alert", "No Alert_Title found.")  
                  End If
              Next
        
          hs.SetDeviceString(1208, Alert_Title, True)
          Catch ex As Exception
      hs.WriteLog ("Weather Alert Error", "Error: " & ex.Message)
      End Try
      End Sub

      Comment


        #4
        Thanks Guys, yea I tried all different ways with upper case lower case and couldn't get it working. Attached is the XML file I am trying to parse.
        Attached Files

        Comment


          #5
          As a alternative suggestion, my Datascraper script can parse the data for you....
          Jon

          Comment


            #6
            Originally posted by jon00 View Post
            As a alternative suggestion, my Datascraper script can parse the data for you....
            And I am using it right now to get what I want (Thank You!), but this for me is to learn scripting a bit

            The problem I have with the datascraper is that the devices it creates doesn't update the modfied time inside of HS (unless I am doing something wrong), so the data never updates on HSTouch because it doesn't see the time change. It updates the time if a value changes, but not if I am just updating the string.

            I have other parsing scripts that are working just fine and update the time on change for a string without issue. So this is confusing me.

            Comment


              #7
              Unless you set the device value to something, a change in devicestring will never update in HSTouch. Are you sure you are saving a numeric value to the device value setting in DataScraper?
              Jon

              Comment


                #8
                Originally posted by jon00 View Post
                Unless you set the device value to something, a change in devicestring will never update in HSTouch. Are you sure you are saving a numeric value to the device value setting in DataScraper?
                I am not because all I am pulling is text. When I use scraping inside a script and only update the string without the value, it updates the time and hence HSTouch without issue.
                Attached Files

                Comment


                  #9
                  Have you set the DeviceMode to 2 ?
                  Jon

                  Comment


                    #10
                    Yea I have it set as 2. Here are two devices, one is updating the time because I have a value associated with it, but the other hasn't updated since it was created, though you can see the data is current. This doesn't have a value, just a string.

                    I do the same thing with a script and create my own device and the time updates with just the string being updated.
                    Attached Files

                    Comment


                      #11
                      Take a note of the device address and then delete it. Recreate a new device with the same address. Does that now work?
                      Jon

                      Comment


                        #12
                        Ok, let me do that. I have a few devices I can test with and see what I can come up with. I will get back to you.

                        Thanks for the help.

                        Still would like to understand my main issue why I can't parse the stupid XML in this thread .

                        Comment


                          #13
                          Does this do what you are after?

                          Sub Main (parm as object)

                          Dim xmlDoc As New XmlDocument()
                          Dim Alert_Title as String

                          Try
                          'Parse XML Data First Layer
                          xmlDoc.Load("C:\Program Files\HomeSeer HS3\Data\weatherXML\NOAlerts1.xml")

                          Dim sNS As String = xmlDoc.DocumentElement.Attributes("xmlns").Value
                          Dim oNsMgr = New XmlNamespaceManager(xmlDoc.NameTable)
                          oNsMgr.AddNamespace("def", sNS)

                          Alert_Title = xmlDoc.SelectSingleNode("/def:feed/def:title", oNsMgr).InnerText)
                          hs.SetDeviceString(1208, Alert_Title, True)
                          hs.writelog("Weather Alert", "Current Alert: " & Alert_Title)

                          Catch ex As Exception : hs.WriteLog ("Weather Alert Error", "Error: " & ex.Message)
                          End Try

                          End Sub

                          Comment


                            #14
                            Originally posted by mrhappy View Post
                            Does this do what you are after?
                            Thank YOU! There was an extra ")" in the one you supplied, and I modifed the xml line to pull the line I needed. It's weird, I tried putting the def: in mine too when I tried using xpath and it still didn't return anything.

                            Thanks again!

                            Code:
                            Sub Main (parm as object)
                            
                            Dim xmlDoc As New XmlDocument()
                            Dim Alert_Title as String
                            
                            Try
                            'Parse XML Data First Layer
                            xmlDoc.Load("C:\Program Files\HomeSeer HS3\Data\weatherXML\NOAlerts1.xml")
                            
                            Dim sNS As String = xmlDoc.DocumentElement.Attributes("xmlns").Value
                            Dim oNsMgr = New XmlNamespaceManager(xmlDoc.NameTable)
                            oNsMgr.AddNamespace("def", sNS)
                            
                            Alert_Title = xmlDoc.SelectSingleNode("/def:feed/def:entry/def:title", oNsMgr).InnerText
                            hs.SetDeviceString(1208, Alert_Title, True)
                            hs.writelog("Weather Alert", "Current Alert: " & Alert_Title)
                            
                            Catch ex As Exception : hs.WriteLog ("Weather Alert Error", "Error: " & ex.Message)
                            End Try
                            
                            End Sub

                            Comment

                            Working...
                            X