I am looking for user friendly library for parsing an xml file. Minimal requirement is I should be able to get default value of attribute if it is not present, with support to extract c++ data types such as boolean. I do not find libxml++ very user friendly, while tinyXML2 is almost Looks like What I am looking for, but it does not support default values and boolean value extraction.

So APIs should look like:

size = element.getAttrAsInt("size", 100); //if sizeis not specified return 100
price = element.getAttrAsDouble("price", 11.1); //if price is not specified return 11.1 

Turning my comment into an answer

You may want to look at pugixml (recommended here):

pugixml is a light-weight C++ XML processing library. It features:

  • DOM-like interface with rich traversal/modification capabilities
  • Extremely fast non-validating XML parser which constructs the DOM tree from an XML file/buffer
  • XPath 1.0 implementation for complex data-driven tree queries
  • Full Unicode support with Unicode interface variants and automatic encoding conversions

The library is extremely portable and easy to integrate and use.

It does cover the attribute handling you are searching for:

pugixml provides several accessors that convert attribute value to some other type:

int          xml_attribute::as_int(int def = 0) const; 
unsigned int xml_attribute::as_uint(unsigned int def = 0) const; 
double       xml_attribute::as_double(double def = 0) const; 
float        xml_attribute::as_float(float def = 0) const; 
bool         xml_attribute::as_bool(bool def = false) const; 
long long    xml_attribute::as_llong(long long def = 0) const;
unsigned long long xml_attribute::as_ullong(unsigned long long def = 0) const;
  • @Floarian: thanks, this is the answer. I have started using it. Sep 4 '15 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.