Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Extract HTML Form InformationDownload an HTML page, parse the HTML, and extract information for the 1st form on the page. Downloads: MS Windows Visual C/C++ Libraries Linux/CentOS C/C++ Libraries MAC OS X C/C++ Libraries Solaris C/C++ Libraries C++ Builder Libraries #include <C_CkHttp.h> #include <C_CkHtmlToXml.h> #include <C_CkXml.h> void ChilkatSample(void) { HCkHttp http; BOOL success; const char * html; HCkHtmlToXml htmlToXml; HCkXml xml; HCkXml formRoot; HCkXml xmlTemp; HCkXml xmlNode; http = CkHttp_Create(); // Any string unlocks the component for the 1st 30-days. success = CkHttp_UnlockComponent(http,"Anything for 30-day trial"); if (success != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); return; } // Send the HTTP GET and return the content in a string. html = CkHttp_quickGetStr(http,"http://www.nature.com/register/"); // Convert the HTML to XML for parsing... htmlToXml = CkHtmlToXml_Create(); // Any string argument automatically begins the 30-day trial. success = CkHtmlToXml_UnlockComponent(htmlToXml,"30-day trial"); if (success != TRUE) { printf("HtmlToXml component unlock failed\n"); return; } // Indicate the charset of the output XML we'll want. // The charset of the original HTML page can be anything. // Chilkat will convert and handle it properly... CkHtmlToXml_putXmlCharset(htmlToXml,"utf-8"); // Set the HTML: CkHtmlToXml_putHtml(htmlToXml,html); // Convert and load into an XML object: xml = CkXml_Create(); CkXml_LoadXml(xml,CkHtmlToXml_toXml(htmlToXml)); success = CkXml_SaveXml(xml,"out.xml"); if (success != TRUE) { printf("%s\n",CkXml_lastErrorText(xml)); return; } // Find the first form tag and navigate to it: formRoot = CkXml_SearchForTag(xml,0,"form"); if (formRoot == 0 ) { printf("No HTML form found!\n"); return; } // Show the form's name, method, and action: printf("Form Name: %s\n",CkXml_getAttrValue(formRoot,"name")); printf("Form Method: %s\n",CkXml_getAttrValue(formRoot,"method")); printf("Form Action: %s\n",CkXml_getAttrValue(formRoot,"action")); printf("----\n"); // Iterate over "input" tags within the form: xmlNode = CkXml_SearchForTag(formRoot,0,"input"); while ((!(xmlNode == 0 ))) { // print the input field's name, type, and current value: // an empty input type indicates the default "text" type printf("Input Name: %s\n",CkXml_getAttrValue(xmlNode,"name")); printf("Input Type: %s\n",CkXml_getAttrValue(xmlNode,"type")); printf("Current Value: %s\n",CkXml_getAttrValue(xmlNode,"value")); printf("----\n"); xmlTemp = xmlNode; xmlNode = CkXml_SearchForTag(formRoot,xmlNode,"input"); CkXml_Dispose(xmlTemp); } // Iterate over "select" tags within the form: xmlNode = CkXml_SearchForTag(formRoot,0,"select"); while ((!(xmlNode == 0 ))) { printf("Select Name: %s\n",CkXml_getAttrValue(xmlNode,"name")); xmlTemp = xmlNode; xmlNode = CkXml_SearchForTag(formRoot,xmlNode,"select"); CkXml_Dispose(xmlTemp); } // Iterate over "textarea" tags within the form: xmlNode = CkXml_SearchForTag(formRoot,0,"textarea"); while ((!(xmlNode == 0 ))) { printf("TextArea Name: %s\n",CkXml_getAttrValue(xmlNode,"name")); xmlTemp = xmlNode; xmlNode = CkXml_SearchForTag(formRoot,xmlNode,"textarea"); CkXml_Dispose(xmlTemp); } CkXml_Dispose(formRoot); CkHttp_Dispose(http); CkHtmlToXml_Dispose(htmlToXml); CkXml_Dispose(xml); } |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.