developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Televisions
Condos For Sale
Domain registration
Baby Photo Contest
Compare Prices
Web Hosting Directory
Computer Hardware
Promotional Gifts
Computer Deals
Holiday Gift Ideas
Online Education
Career Education
Promotional Pens
Imprinted Gifts

 


Web Devs:
Moonlight as a Game Developer and Win Cool Prizes by Accepting the RIA Run Challenge

Now, your mission--should you choose to accept: Take your shot at gaming stardom if you think you might have what it takes to build a cool RIA game and you could win an Xbox 360 or other fabulous prizes. Hurry! You only have until May 15, 2008 to enter. »

 
Article:
Leveraging Your Flash Development with Silverlight

You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here. »

 
Article:
What Does it Take to Build the Best RIA?

With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application. »

 
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Libxml2: Everything You Need in an XML Library
By Victor Volkman

Go to page: 1  2  Next  

Libxml2 is the XML parser and toolkit written in the C language and is freely available for integration into your apps via the easy-to-digest MIT License. Libxml2 was originally developed for the Gnome project, but doesn't have any dependencies on it or even the Linux platform. This tool is known to be highly portable and is in use by many teams on Linux, Unix, Win32/Win64, Cygwin, MacOS, MacOS/X, and most other platforms, including embedded systems. Even though Libxml2 was written in C, there are an abundance of language bindings available including bindings for Python, Perl, C++, C#, PHP, Pascal, Ruby, and Tcl.

As you know, XML itself is a metalanguage used to design markup languages. That is to say, it is a grammar where semantics and structure are added to the content using extra "markup" information enclosed between angle brackets "<" and ">". HTML certainly is the most well-known markup language and the specification of HTML 4.0 can be fully articulated using an XML Document Type Definition (DTD).

Of course, just saying something is an XML parser doesn't imply all that much. You have to enumerate both how much and what you're going to support. As such, Libxml2 implements a number of existing standards related to markup languages. I won't bore you with the whole laundry list, but the majors are: XML standard 1.0 including Namespaces, Base, URI, XPointer, XInclude, XPath, HTML 4.0 parser, Canonical XML 1.0, XML Schemas Part 2, xml:id, and XML Catalog working drafts. In most cases, libxml2 tries to implement the specifications in a relatively strictly compliant way. Libxml2 has passed all 1800+ tests from the OASIS XML Testsuite.

XML documents aren't always sitting around on your local filesystem for perusal, so Libxml2 includes basic FTP and HTTP clients so you don't have to write an extra layer of code just to find your documents. Libxml2 exports Push (progressive) and Pull (blocking) type parser interfaces for both XML and HTML. Libxml2 can do DTD validation at parse time, using a parsed document instance, or with an arbitrary DTD. Sister projects provide some additional goodies like XSLT 1.0 (from libxslt) and a DOM2 implementation is also in the works.

Let's Get This Parser Started!

Although you are certainly welcome to recompile the source to meet your own project requirement quirks, I found the simplest way to get parsing was through Igor Zlatkovic's dedicated libxml Win32 resource page. In addition to DLL downloads, you will also find C#, Perl (Apache), and Pascal language bindings at the bottom of Zlatkovic's page. Zlatkovic has packaged Libxml2 and related tools so you can simply take the subset you really need:

  • libxml2, the XML parser and processor
  • libxslt, the XSL and EXSL Transformations processor
  • xmlsec, the XMLSec and XMLDSig processor
  • xsldbg, the XSL Transformations debugger
  • openssl, the general crypto toolkit
  • iconv, the character encoding toolkit
  • zlib, the compression toolkit

Figure 1: libxml package dependencies

For example, libxml depends on iconv and zlib. If you run the included xmllint.exe or xmlcatalog.exe, you simply will discover that you need iconv.dll (as promised in the dependency chart). Be advised that Zlatkovic's downloads don't include the sample programs and data files, however.

For purposes of this article, I used Zlatkovic's distribution of libxml2 2.6.30+, iconv 1.9.2., and zlib 1.2.3.

How to Parse a Tree with Libxml2

You'll only look at the Document Object Model (DOM) parser because that is inherently more complex than the Simple API for XML (SAX). As you recall, the DOM model gives you complete tree navigation at the cost of maintaining the whole XML file in memory. If you're not modifying the tree as it's being parsed, SAX can be significantly less overhead.



Click here for a larger image.

Figure 2: DOM tree example

Specifically, you'll dissect the tree1.c example program and identify some common programming paradigms used. The purpose of this program is to parse a file to a tree, use xmlDocGetRootElement() to get the root element, and then walk the document and print all the element names in document order. This is about the easiest non-trivial sort of thing you can do in XML. For simplicity's sake, you'll assume that the XML file you want to parse is the first argument on the command line and output will go to stdout (console). Program listing follows:

 1 #include <stdio.h>
 2 #include <libxml/parser.h>
 3 #include <libxml/tree.h>
 4
 5 static void print_element_names(xmlNode * a_node)
 6 {
 7    xmlNode *cur_node = NULL;
 8
 9    for (cur_node = a_node; cur_node; cur_node =
         cur_node->next) {
10       if (cur_node->type == XML_ELEMENT_NODE) {
11          printf("node type: Element, name: %s\n",
               cur_node->name);
12       }
13       print_element_names(cur_node->children);
14    }
15 }
16
17 int main(int argc, char **argv)
18 {
19    xmlDoc *doc = NULL;
20    xmlNode *root_element = NULL;
21
22    if (argc != 2)  return(1);
23
24    LIBXML_TEST_VERSION    // Macro to check API for match with
                             // the DLL we are using
25
26    /*parse the file and get the DOM */
27    if (doc = xmlReadFile(argv[1], NULL, 0)) == NULL){
28       printf("error: could not parse file %s\n", argv[1]);
29       exit(-1);
30       }
31
32    /*Get the root element node */
33    root_element = xmlDocGetRootElement(doc);
34    print_element_names(root_element);
35    xmlFreeDoc(doc);       // free document
36    xmlCleanupParser();    // Free globals
37    return 0;
38 }

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


XML Archives

Data Sheet: IBM Information Server Blade
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Whitepaper: XML Processing in Applications--Take the Next Step

Access FREE HP High-Availability Solutions for Exchange 2007 Tools:
Whitepaper:
Backup and Recovery Best Practices for Microsoft Exchange Server 2007 with HP
Whitepaper:
Best Practices for HP Servers and HP Enterprise Virtual Array in a Microsoft Exchange
Whitepaper:
Optimizing HP Servers with Microsoft SQL Server 2008


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES