chipKIT® Development Platform

Inspired by Arduino™

problem with read xml

Created Fri, 22 Mar 2013 01:07:23 +0000 by eraldo


eraldo

Fri, 22 Mar 2013 01:07:23 +0000

Hi ereryone, i´m having trouble with my application with ethernet. I´m trying to read some datas that come from a web server and use them to change some variables with it. i tried to use textfinder.h from arduino playground but an error appear when I´m going to compile, it says something about not such file... I´m testing the code exemple ChipKITWebClient from the ethernet library from the ethernet shield. this is what I receive.> connecting... connected HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Cache-Control: no-cache Content-Type: text/xml;charset=ISO-8859-1 Date: Fri, 22 Mar 2013 00:48:49 GMT Connection: close <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <estufa> <irrigacao>3.13</irrigacao> <temperatura>40.1</temperatura> <umidade>0.0</umidade> </estufa> disconnecting.

So, i wanna use these values from irrigation, temperature to put them in my routines and make some actions. Someone know how can i read theses values and put them into variables thanks


majenko

Fri, 22 Mar 2013 09:17:57 +0000

The XML is so simple in this situation that treating it as XML is not really needed.

All you need do is take it a line at a time, find the > character, and convert the line from that point onwards to a float (using atof()). If you need to identify the line, you can look at the string from the second character up to the > character to find which variable it is, then use strcmp (or strncmp) to compare it to constant values.


eraldo

Fri, 22 Mar 2013 18:11:59 +0000

Ok thanks, but i tried to take line at line, i put

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  for ( int i = 0; i &lt; 20; i ++){
  if (client.available()) {
    
    c = client.read();
    if ( c == '&gt;'){
     x = true; 
     continue;
    }
     if (x){
     test[ind] = c;}
   
break;  
  }
  }
   Serial.print(test[ind]);
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      Ethernet.PeriodicTasks();
  }
}

and this is what i got > <estufa> <irrigacao>3.13</irrigacao> <temperatura>40.1</temperatura> <umidade>0.0</umidade> </estufa> disconnecting.

i dont understand how can i get the values and put them into my variables


jmlynesjr

Sat, 23 Mar 2013 00:26:58 +0000

You have your data in an array already.

Read up on the string functions and use string-index and sub-string to walk through the array and pull out the data values.

James

/*
  String indexOf() and lastIndexOf() functions
 
 Examples of how to evaluate, look for, and replace characters in a String
 
 created 27 July 2010
 modified 2 Apr 2012
 by Tom Igoe
 
**http://arduino.cc/en/Tutorial/StringIndexOf**
 
 This example code is in the public domain. 
 */

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // send an intro:
  Serial.println("\n\nString indexOf() and lastIndexOf()  functions:");
  Serial.println();
}

void loop() {
  // indexOf() returns the position (i.e. index) of a particular character
  // in a string. For example, if you were parsing HTML tags, you could use it:
  String stringOne = "&lt;HTML&gt;&lt;HEAD&gt;&lt;BODY&gt;";
  int firstClosingBracket = stringOne.indexOf('&gt;');
  Serial.println("The index of &gt; in the string " + stringOne + " is " + firstClosingBracket);

  stringOne = "&lt;HTML&gt;&lt;HEAD&gt;&lt;BODY&gt;";
  int secondOpeningBracket = firstClosingBracket + 1;
  int secondClosingBracket = stringOne.indexOf('&gt;', secondOpeningBracket );
  Serial.println("The index of  the second &gt; in the string " + stringOne + " is " + secondClosingBracket);

  // you can also use indexOf() to search for Strings:
  stringOne = "&lt;HTML&gt;&lt;HEAD&gt;&lt;BODY&gt;";
  int bodyTag = stringOne.indexOf("&lt;BODY&gt;");
  Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);

  stringOne = "&lt;UL&gt;&lt;LI&gt;item&lt;LI&gt;item&lt;LI&gt;item&lt;/UL&gt;";
  int firstListItem = stringOne.indexOf("&lt;LI&gt;");
  int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
  Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);

  // lastIndexOf() gives you the last occurrence of a character or string:
  int lastOpeningBracket = stringOne.lastIndexOf('&lt;');
  Serial.println("The index of the last &lt; in the string " + stringOne + " is " + lastOpeningBracket);

  int lastListItem  = stringOne.lastIndexOf("&lt;LI&gt;");
  Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);


  // lastIndexOf() can also search for a string:
  stringOne = "&lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt;&lt;p&gt;Ipsem&lt;/p&gt;&lt;p&gt;Quod&lt;/p&gt;";
  int lastParagraph = stringOne.lastIndexOf("&lt;p");
  int secondLastGraf = stringOne.lastIndexOf("&lt;p", lastParagraph - 1);
  Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);

  // do nothing while true:
  while(true);
}

eraldo

Mon, 25 Mar 2013 18:20:51 +0000

so this char c = client.read(); returns an array, can i use a for command to put the datas into a string to use the methods of the String object??