byGary Roberts| Updated: 03/02/2016 | Comments: 1
Would it be helpful to include data from a reputable source with your own data? If you have permission to use another source’s data for free or by agreement, how can you easily extract the specific data you want to use without doing a lot of coding?
In this article, I’ll show you how you can use an instruction in the CRBasic programming language to reap the benefit of a trusted source’s data while saving yourself a lot of time and effort. For example, you might want to pull data from a known, good source. The data may be stored on a government server or another source at no cost, and be offered in several different formats, including the eXtensible Markup Language (XML).
To highlight the six parsing steps, I’ll walk you through an example. In this example, we need the temperature in Fahrenheit from a NOAA weather station at an airport (KLGU). We want to use this temperature data with the data from a weather station that is just a couple of miles away.
The airport’s NOAA weather station data is hosted on NOAA web servers and is available for public use. By doing a quick search of their website, we found the airport’s weather station data here:http://w1.weather.gov/xml/current_obs/klgu.xml.。数据包括温度,包括XML格式的每小时,类似于此:
要查看实际的XML代码,我们必须右键单击网页并选择查看页面源代码从菜单中。我们看到的看起来类似于这个:
There is a lot of data in the XML code. If we used the normal programming methods, it would take us quite some time to do the coding. Fortunately, CRBasic has anXML.Parse()我们可以使用的指导为我们节省几个小时的键盘时间。
To get started with theXML.Parse()instruction, we need a few declared variables (constants). TheXML.Parse()instruction uses these variables to know where it is in the XML file, if an error occurred, and if it is finished parsing.
'返回XMLPARSE的值。我们使用这些来跟踪我们所在的位置,如果我们有错误。const xml_too_many_namespaces = -3'在解析元素const xml xml_nested_too_deep = -2'时遇到的太多名称空间声明= dup xml_syntax_error_or_failed = -1'xml语法错误或xmlparse失败const xml_unrecognized_error_condition = 0'无法识别错误条件const xml_start =1'xml元素的开始xml xml_attribute_read = 2'XML属性读取。const XML_END_OF_ELEMENT = 3'XML元素结尾CONST XML_END_OF_DOCUMENT = 4'eN的XML文档终止。'xmlparse max设置,所以我们不使用所有数据记录器的内存const xml_max_depth = 10 const xml_max_namespaces = 3
We also need a variable in which to store the results from theXML.Parse()instruction:
民众noaa_air_temperature_f
此外,我们需要更多的变量XML.Parse()在解析XML文件时使用的指令。我们可以使用Dimvariable declarations, but let’s use民众有助于我们的故障排除的变量。
民众xml_attribute_name As String Public xml_attribute_namespace As String * 100 Public xml_data As String * 3000 Public xml_element_name As String * 50 Public xml_element_namespace As String * 30 Public xml_response_code Public xml_state Public xml_value As String * 50
要从服务器中检索XML文件,我们将使用HTTPGet()instruction. For this instruction, we need to add a couple of variables:
民众xml_http_header As String * 300 Public xml_http_socket As Long
To get the XML file from the server and load it into theXML_DATA.变量,我们需要在慢速序列扫描中以某处添加以下代码:
xml_http_header = "" xml_http_socket = HTTPGet("http://w1.weather.gov/xml/current_obs/KLGU.xml", xml_data, xml_http_header) TCPClose(xml_http_socket) 'Close our connection to the web server.
让我们让XML.Parse()instruction do its work, we add a while loop (using the虽然/ wend.instruction) and set the initialxml_response_code.。
xml_response_code = xml_start'告诉xmlparse我们刚刚开始。而((xml_response_code> XML_UNRECOGNIZED_ERROR_CONDITION)AND(xml_response_code <> XML_END_OF_DOCUMENT))xml_response_code = XMLPARSE(xml_data,xml_value,xml_attribute_name,xml_attribute_namespace,_ xml_element_name,xml_element_namespace,XML_MAX_DEPTH,XML_MAX_NAMESPACES)如果xml_response_code = XML_END_OF_ELEMENT AND xml_element_name = “temp_f” 那么noaa_air_temperature_f = xml_value结束Wend.
虽然这吧XML.Parse()instruction is running the while loop, it is searching for the element namedtemp_f。When theXML.Parse()指令查找此元素,它会分配位于之间的值
We are now getting the value we wanted (temperature in Fahrenheit) from the NOAA station, and we can include it with our own weather station data.
If you have a CR1000, CR3000, CR800, CR850, or CR6 datalogger with an Ethernet interface, you can下载并运行此程序的工作副本。
推荐的佛r You:For an explanation of the different parts of an XML file, review theW3Schools.com提供的“XML树”部分。此Web开发人员网站具有基本教程,其详细介绍可用于XML的不同元素,名称空间和属性。 |
I hope this information was helpful to you. If you have any questions, please post them below.
注释
OnAMission|2016/03/29 2016年上午10:09
Thanks for the great blog post! These advanced guides are very useful so please keep them comming!
Pleaselog in or register评论。