Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) HTML Table to CSV

Demonstrates a method for converting an HTML table to a CSV file.

Note: This example requires Chilkat v9.5.0.77 or greater.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoHttp.h>
#import <CkoBinData.h>
#import <CkoHtmlToXml.h>
#import <CkoStringBuilder.h>
#import <CkoXml.h>
#import <NSString.h>
#import <CkoCsv.h>

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// First download the HTML containing the table
CkoHttp *http = [[CkoHttp alloc] init];
CkoBinData *bdHtml = [[CkoBinData alloc] init];

BOOL success = [http QuickGetBd: @"https://example-code.com/data/etf_table.html" binData: bdHtml];
if (success != YES) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

// Convert to XML.
CkoHtmlToXml *htx = [[CkoHtmlToXml alloc] init];
[htx SetHtmlBd: bdHtml];

CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
[htx ToXmlSb: sbXml];

CkoXml *xml = [[CkoXml alloc] init];
[xml LoadSb: sbXml autoTrim: YES];

// Remove attributes and sub-trees we don't need.
// (In other words, we're getting rid of clutter...)
int numRemoved = [[xml PruneTag: @"thead"] intValue];
numRemoved = [[xml PruneAttribute: @"style"] intValue];
numRemoved = [[xml PruneAttribute: @"class"] intValue];

// Scrub the element and attribute content.
[xml Scrub: @"ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside"];

// Let's see what we have...
NSLog(@"%@",[xml GetXml]);

// We have the following XML.
// Copy this XML into the online tool at Generate Parsing Code from XML
// as a starting point for accessing the data..

// <?xml version="1.0" encoding="utf-8"?>
// <root>
//     <html>
//         <head>
//             <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
//         </head>
//         <body text="#000000" bgcolor="#FFFFFF">
//             <div>
//                 <div>
//                     <table role="grid" data-scrollx="true" data-sortdirection="desc" data-sorton="-1"/>
//                 </div>
//             </div>
//             <div>
//                 <table id="topHoldingsTable" role="grid" data-scrollx="true" data-sortdirection="desc" data-sorton="-1">
//                     <tbody>
//                         <tr role="row">
//                             <td>
//                                 <text>ITUB4</text>
//                             </td>
//                             <td>
//                                 <text>ITAU UNIBANCO HOLDING PREF SA</text>
//                             </td>
//                             <td>
//                                 <text>Financials</text>
//                             </td>
//                             <td>
//                                 <text>Brazil</text>
//                             </td>
//                             <td>
//                                 <text>10.94</text>
//                             </td>
//                             <td>
//                                 <text>998,954,813.73</text>
//                             </td>
//                         </tr>
//                         <tr role="row">
//                             <td>
//                                 <text>BBDC4</text>
//                             </td>
//                             <td>
//                                 <text>BANCO BRADESCO PREF SA</text>
//                             </td>
//                             <td>
//                                 <text>Financials</text>
//                             </td>
//                             <td>
//                                 <text>Brazil</text>
//                             </td>
//                             <td>
//                                 <text>9.01</text>
//                             </td>
//                             <td>
//                                 <text>822,164,622.75</text>
//                             </td>
//                         </tr>
// 			...
// 			...
// 			...
//                     </tbody>
//                 </table>
//             </div>
//         </body>
//     </html>
// </root>

// 
// This is the code generated by the online tool:
// 
int i;
int count_i;
NSString *table_role = 0;
NSString *table_data_scrollx = 0;
NSString *table_data_sortdirection = 0;
NSString *table_data_sorton = 0;
NSString *table_id = 0;
int j;
int count_j;
NSString *tr_role = 0;
int k;
int count_k;
NSString *tagPath = 0;
NSString *text = 0;

i = 0;
count_i = [[xml NumChildrenHavingTag: @"html|body|div"] intValue];
while (i < count_i) {
    xml.I = [NSNumber numberWithInt: i];
    table_role = [xml ChilkatPath: @"html|body|div[i]|div|table|(role)"];
    table_data_scrollx = [xml ChilkatPath: @"html|body|div[i]|div|table|(data-scrollx)"];
    table_data_sortdirection = [xml ChilkatPath: @"html|body|div[i]|div|table|(data-sortdirection)"];
    table_data_sorton = [xml ChilkatPath: @"html|body|div[i]|div|table|(data-sorton)"];
    table_id = [xml ChilkatPath: @"html|body|div[i]|table|(id)"];
    table_role = [xml ChilkatPath: @"html|body|div[i]|table|(role)"];
    table_data_scrollx = [xml ChilkatPath: @"html|body|div[i]|table|(data-scrollx)"];
    table_data_sortdirection = [xml ChilkatPath: @"html|body|div[i]|table|(data-sortdirection)"];
    table_data_sorton = [xml ChilkatPath: @"html|body|div[i]|table|(data-sorton)"];
    j = 0;
    count_j = [[xml NumChildrenHavingTag: @"html|body|div[i]|table|tbody|tr"] intValue];
    while (j < count_j) {
        xml.J = [NSNumber numberWithInt: j];
        tr_role = [xml ChilkatPath: @"html|body|div[i]|table|tbody|tr[j]|(role)"];
        k = 0;
        count_k = [[xml NumChildrenHavingTag: @"html|body|div[i]|table|tbody|tr[j]|td"] intValue];
        while (k < count_k) {
            xml.K = [NSNumber numberWithInt: k];
            text = [xml GetChildContent: @"html|body|div[i]|table|tbody|tr[j]|td[k]|text"];
            k = k + 1;
        }

        j = j + 1;
    }

    i = i + 1;
}

// Let's modify the above code to build the CSV.
CkoCsv *csv = [[CkoCsv alloc] init];
[csv SetColumnName: [NSNumber numberWithInt: 0] columnName: @"Ticker"];
[csv SetColumnName: [NSNumber numberWithInt: 1] columnName: @"Name"];
[csv SetColumnName: [NSNumber numberWithInt: 2] columnName: @"Sector"];
[csv SetColumnName: [NSNumber numberWithInt: 3] columnName: @"Country"];
[csv SetColumnName: [NSNumber numberWithInt: 4] columnName: @"Weight"];
[csv SetColumnName: [NSNumber numberWithInt: 5] columnName: @"Notional Vaue"];

i = 0;
count_i = [[xml NumChildrenHavingTag: @"html|body|div"] intValue];
while (i < count_i) {
    xml.I = [NSNumber numberWithInt: i];
    j = 0;
    count_j = [[xml NumChildrenHavingTag: @"html|body|div[i]|table|tbody|tr"] intValue];
    while (j < count_j) {
        xml.J = [NSNumber numberWithInt: j];
        k = 0;
        count_k = [[xml NumChildrenHavingTag: @"html|body|div[i]|table|tbody|tr[j]|td"] intValue];
        while (k < count_k) {
            xml.K = [NSNumber numberWithInt: k];
            [csv SetCell: [NSNumber numberWithInt: j] col: [NSNumber numberWithInt: k] content: [xml GetChildContent: @"html|body|div[i]|table|tbody|tr[j]|td[k]|text"]];
            k = k + 1;
        }

        j = j + 1;
    }

    i = i + 1;
}

[csv SaveFile: @"qa_output/brasil_etf.csv"];
NSString *csvStr = [csv SaveToString];
NSLog(@"%@",csvStr);

// Our CSV looks like this:
// Ticker,Name,Sector,Country,Weight,Notional Vaue
// ITUB4,ITAU UNIBANCO HOLDING PREF SA,Financials,Brazil,10.94,"998,954,813.73"
// BBDC4,BANCO BRADESCO PREF SA,Financials,Brazil,9.01,"822,164,622.75"
// VALE3,CIA VALE DO RIO DOCE SH,Materials,Brazil,8.60,"785,290,260.07"
// PETR4,PETROLEO BRASILEIRO PREF SA,Energy,Brazil,5.68,"518,124,434.10"
// PETR3,PETROBRAS,Energy,Brazil,4.86,"443,254,438.53"
// B3SA3,B3 BRASIL BOLSA BALCAO SA,Financials,Brazil,4.57,"417,636,740.16"
// ABEV3,AMBEV SA,Consumer Staples,Brazil,4.57,"417,216,913.63"
// BBAS3,BANCO DO BRASIL SA,Financials,Brazil,3.25,"296,921,232.15"
// ITSA4,ITAUSA INVESTIMENTOS ITAU PREF SA,Financials,Brazil,2.90,"265,153,684.52"
// LREN3,LOJAS RENNER SA,Consumer Discretionary,Brazil,2.25,"205,832,175.98"
// 
 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.