software,

Apache Xerces on Ubuntu 14.04

Follow Jan 08, 2016 · 4 mins read

we will look at installation and basic usage of Xerses XML Parser on Ubuntu 14.04 platform

Apache Xerces Installation on Ubuntu 14.04

In this article we will look at the installation of Apache Xerces C++ Installation on Ubuntu 14.04

Apache Xerces™ Project

The Apache Xerces™ Project is responsible for software licensed to the Apache Software Foundation intended for the creation and maintenance of XML parsers and related software components

Apache Xerces C++ - A processor for parsing, validating, serializing and manipulating XML, written in C++.

Xerces-C++ is a validating XML parser written in a portable subset of C++ providing high performance, modularity, and scalability.

Xerces-C++ makes it easy to give your application the ability to read and write XML data. A shared library is provided for parsing, generating, manipulating, and validating XML documents. Xerces-C++ is faithful to the XML 1.0 and 1.1 recommendations and many associated standards.

License

The Xerces-C++ Version 3.1.2 is available in both source distribution and binary distribution. Xerces-C++ is made available under the Apache Software License, Version 2.0 which makes it suitable for commercial use.

Download and Installation

The sources and binaries can be found at download link http://xerces.apache.org/xerces-c/download.cgi

The Xerces-C++ source is available in the source distribution: xerces-c-3.1.2.tar.gz

Install the Xerces-C++ source distribution xerces-c-3.1.2.tar.gz by extracting the files from the compressed archive:

gzip -d xerces-c-3.1.2.tar.gz
tar -xf xerces-c-3.1.2.tar

This creates the xerces-c-3.1.2 sub-directory containing the Xerces-C++ source distribution.

To build the Xerces-C++ source after installation, please follow the Build Instructions.

For building on UNIX and UNIX-like (GNU/Linux, Max OS X, Cygwin, MinGW-MSYS) platforms Xerces-C++ uses the GNU automake-based build systems and requires that you have GNU make installed.

As with all automake-based projects the build process is divided into two parts: configuration and building. The configuration part is performed using the configure script that can be found in the xerces-c-3.1.2 directory. The build part is performed by invoking make.

configure
make

Once the configuration part is complete you can run make. Running make from the xerces-c-3.1.2 directory builds Xerces-C++ library and examples. The library is placed into the src/.libs directory.

Finally, to install the library,header files and examples you can run make install (or gmake install). To change the installation directory, use the –prefix configure option.

The libraries are installed in /usr/local/lib subdirectory while include files are installed in /usr/local/include/xercesc/ subdirectory

Options By default configure selects both shared and static libraries. You can use the --disable-shared and --disable-static options to avoid building the version you don’t need.

If you need to specify compiler executables that should be used to build Xerces-C++, you can set the CC and CXX variables when invoking configure. Similarly, if you need to specify additional compiler or linker options, you can set the CFLAGS, CXXFLAGS, and LDFLAGS variables. For example:

./configure --disable-static CC=gcc-4.3 CXX=g++-4.3 CFLAGS=-O3 CXXFLAGS=-O3
make

Programming

Initialization

Independent of the API you want to use, DOM, SAX, or SAX2, your application must initialize the Xerces system before using the API, and terminate it after you are done.

#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.

using namespace xercesc;

int main(int argc, char* argv[])
{
  try {
    XMLPlatformUtils::Initialize();
  }
  catch (const XMLException& toCatch) {
    // Do your failure processing here
    return 1;
  }

  // Do your actual work with Xerces-C++ here.

  XMLPlatformUtils::Terminate();

  // Other terminations and cleanup.
  return 0;
}

XMLPlatformUtils::Initialize() and XMLPlatformUtils::Terminate must be called at least once in each process.

Parsing an XML File

In order to use Xerces-C++ to parse XML files using DOM, you can create an instance of the XercesDOMParser class


       XercesDOMParser* parser = new XercesDOMParser();
        parser->setValidationScheme(XercesDOMParser::Val_Always);
        parser->setDoNamespaces(true);    // optional

        ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
        parser->setErrorHandler(errHandler);

        char* xmlFile = "x1.xml";

        try {
            parser->parse(xmlFile);
        }
        catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            return -1;
        }
        catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            return -1;
        }
        catch (...) {
            cout << "Unexpected Exception \n" ;
            return -1;
        }

        delete parser;
        delete errHandler;
        return 0;

References

  • https://xerces.apache.org/xerces-c/build-3.html
  • http://xerces.apache.org/xerces-c/
  • http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c
Written by