Convert HTML content to XML Convert HTML content to XML Convert HTML content to XML

In this write up i will illustrating how we can convert HTML content to XML. Remember one thing that with HTML into picture things will be more or less static. The reason i had to get this code was i had a requirement wherein i had to read a table from a website and store its data into AX table. Below is the code that does the same

static void convertHTML2XML(Args _args)
{
WinInet wi = new WinInet();
TextBuffer tb = new TextBuffer();
XML xmlString;
str page;
str filename;
int handle;
int i;
str tmpHTml;
container con;
str tmpString;
int tillWhere;
boolean firstSearch = true;
int fields = 0;
void formXML(int _i, str _value)
{
switch(_i)
{
case 1, 2: break;
case 3: xmlString += ‘’+_value+’’;break;
case 4: xmlString += ‘’+_value+’’;break;
case 5: xmlString += ‘’+_value+’’;break;
case 6: xmlString += ‘’+_value+’’;break;
case 7: xmlString += ‘’+_value+’
’;break;
}
if (_i == 7)
{
fields = 0;
}
}
;
xmlString += ‘’;
handle = wi.internetOpenUrl(‘http://finanzas.inicia.es/cotizaciones/tiposInteres.php?Sesion_BolsamaniaES=2h5opigq5iuhad7te24ip1jpq1′);
if (handle)
{
page = wi.internetReadFile(handle);
//info(page);
}
con = str2con(web::stripHTML(page,true,true),’\n’);
for (i = 1; i < conlen(con); i++) { tmpString = strltrim(strreplace(conpeek(con, i),’\t’,”)); if (firstSearch) { if (strstartswith(tmpString,’Fecha’)) { tillWhere = i; firstSearch = false; fields = 1; } } tmpString = strreplace(tmpString,’\t’,”); if (strstartsWith(tmpString,’Tipos de interés de los bancos centrales’)) break; if (fields) { if (tmpString) { if (!strstartswith(tmpString,’Fecha’)) { formXML(fields, tmpString); fields ++; } } } } tb.setText(xmlString + ‘
’);
tb.toFile(‘E:\\ExchangeRates.xml’); // use xmldocument instead
wi.internetCloseHandle(handle);
}
Note that I had to hardcode the column names to convert the same to XML. Do write in your comments if you can suggest better alternatives.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.