take IP from xml on SD card

Hello all,
I'm trying to extract the imformação ip allocated to an XML file on the SD card and load it on arduino.
I'm using an Arduino Uno REV3 and an Ethernet Shield.
I can read the content on the card but I can not isolate the text XML.

this is my XML file
#################################################


192
168
0
20


################################################

any one can tell me the best way to do this? exist a command or library to extract text beetween and ?

Tks

I can read the content on the card but I can not isolate the text XML.

How? That dictates how easy or hard parsing the data is going to be.

Hey PaulS tks for help.
in fact i try so many diferent ways, that i don´t know the better to post.
I try using tynixml but i didn´t find a helpfull documentation.

I try a string search to but it was too big.

I´m begginner with arduino so if you, or anyone else have any idea about how to do this, I appreciate

Why do you want to do this? If you simply need to set the IP address from a config file, a simple comma separated file would be easier for the Arduino to parse. Do you have other constraints that mean you must use XML?

yeah, i will use this xml to pass to a base code on arduino many parameter of communication.

yeah, i will use this xml to pass to a base code on arduino many parameter of communication.

The point is that parsing XML files is hard. Parsing csv files is easy. If you have a choice, don't use XML.

Hey I can do this, with this code..

void readFile()
{
char chtr[150] = "";
File myFile;
int Position;

Serial.println("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization SD Card done.");

myFile=SD.open("conf.xml");
Serial.println("set ip address:");

int i=0;
while(myFile.available())
{
chtr = myFile.read();

  • i++;*

  • }*

  • myFile.close();*

  • String Conf = chtr;*

  • Serial.println(Conf);*

  • int positionOct = Conf.indexOf("")+5;*

  • int positionEndOct = Conf.indexOf("");*

  • String FirstOct = Conf.substring(positionOct,positionEndOct);*

  • char sFirstOct[3];*

  • //int nFirstOct = FirstOct.Toint();*

  • FirstOct.toCharArray(sFirstOct, 5);*

  • Serial.println(sFirstOct);*

  • positionOct = Conf.indexOf("")+5;*

  • positionEndOct = Conf.indexOf("");*

  • String SecondOct = Conf.substring(positionOct,positionEndOct);*

  • char sSecondOct[3];*

  • SecondOct.toCharArray(sSecondOct, 5);*

  • Serial.println(sSecondOct);*

  • positionOct = Conf.indexOf("")+5;*

  • positionEndOct = Conf.indexOf("");*

  • String ThirdOct = Conf.substring(positionOct,positionEndOct);*

  • char sThirdOct[3];*

  • ThirdOct.toCharArray(sThirdOct, 5);*

  • Serial.println(sThirdOct);*

  • positionOct = Conf.indexOf("")+5;*

  • positionEndOct = Conf.indexOf("");*

  • String FourthOct = Conf.substring(positionOct,positionEndOct);*

  • char sFourthOct[3];*

  • FourthOct.toCharArray(sFourthOct, 5);*

  • Serial.println(sFourthOct);*
    // Entre com um endereço MAC para o arduino.
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    // Entre com um endereço IP para o arduino:
    IPAddress localip = (192,168,0,18);

localip[0] = atoi(sFirstOct);
localip[1] = atoi(sSecondOct);
localip[2] = atoi(sThirdOct);
localip[3] = atoi(sFourthOct);
Serial.println(localip[0]);
Serial.println(localip[1]);
Serial.println(localip[2]);
Serial.println(localip[3]);
Serial.println(localip);

  • Ethernet.begin(mac, localip);*
    }
    But MY terminal response is.
    Initializing SD card...
    initialization SD Card done.
    set ip address:
    My XML File
    <?xml version="1.0" ?>
  • *
  • 192*
  • 168*
  • 0*
  • 20*
  • *

    end My XML File
    Char Response
    192
    168
    0
    20
    End Char Response
    Int Response
    144
    144
    0
    20
    End Int Response
    144.144.0.20
    Wy the two first octects change to 144? anyone can help?

After populating a char array, why are you wrapping it in a String? There is no reason for that. People developed extensive text processing applications without the crutch of a String class for 40 years before it was created.

By the way, that still isn't the proper way to post code.

  char sFirstOct[3];
  //int nFirstOct = FirstOct.Toint();
  FirstOct.toCharArray(sFirstOct, 5);

Wrong! Look at the toCharArray documentation again. The 3rd argument is wrong. There is NOT room in the array for 5 characters.

Hey PaulS,
Tks for your answer.
in fact you are right, the 5 should be a 4, because a null caracter

I change to String because is very simple to use and extract caracter using this function.
I need a way to convert the String "192" to a int 192. but I didn´t find this funtion in String.

I only find the atoi function that why I use toCharArray function.

but the two first octects give me the same number as result 144.

Did you know why?

in fact you are right, the 5 should be a 4, because a null caracter

No. The 5 should be a 3, because that is how big the array is. Now, clearly, the arrays are too small, so they need to be made bigger, and the new size used where the 5 is now.

I need a way to convert the String "192" to a int 192. but I didn´t find this funtion in String.

Not to encourage the use of the String class, but it does have a toInt() method.

but the two first octects give me the same number as result 144.

Did you know why?

You are writing outside the bounds of your array(s).

No. The 5 should be a 3, because that is how big the array is. Now, clearly, the arrays are too small, so they need to be made bigger, and the new size used where the 5 is now

I agree, but if I set to 3 the last number don´t go to array, in somewhere I read that in this function you need a null caracter in the end of array, don´t know why.

Not to encourage the use of the String class, but it does have a toInt() method.

can you show me a example of this toInt() method? I don´t find it on references.

Thanks

I agree, but if I set to 3 the last number don´t go to array, in somewhere I read that in this function you need a null caracter in the end of array, don´t know why.

You do need a NULL. The point is that 4 characters ('1', '0', '2', '3') and a NULL will NOT fit in a 3 element array. You MUST make the arrays large enough.

String crap = "14";
int crapAsInt = crap.toInt();
Serial.print(crapAsInt);

You'll see 14 on the Serial Monitor.