tags:

views:

111

answers:

9

Under Windows to parse XML one can use the COM based XML API but I wondered if there is any C++ XML libraries out there I can use for this purpose, without needing to get into the intricacies of COM?

+2  A: 

Yes of course.

If you don't need validation, I'd suggest TinyXML++ which is also very simple to use :

    ticpp::Document doc(pcFilename);
    doc.LoadFile();

    // parse through all elements
    std::string strName;
    std::string strValue;
    ticpp::Iterator<ticpp::Element> child;
    for(child = child.begin(doc.FirstChildElement()); child != child.end(); child++)
    {
            // parse through all the attributes of this fruit
            ticpp::Iterator< ticpp::Attribute > attribute;
            for(attribute = attribute.begin(child.Get()); attribute != attribute.end(); attribute++)
            {
                    attribute->GetName(&strName);
                    attribute->GetValue(&strValue);
                    std::cout << strName << ": " << strValue << std::endl;
            }
            std::cout << std::endl;
    }

If you need validation, consider using XercesC++.

Samuel_xL
+1  A: 

I've used XercesC++ in the past to good effect. I recently used MSXML which was all pain in comparison.

graham.reeds
A: 

AFAIK, the most efficient open-source parser library for C, C#, C++, and Java nowadays is VTD-XML. In terms of performance, it beats DOM, SAX, and XPath. I has converted the library to Delphi for my private project, and I found that VTD-XML is just like what it claims. See VTD-XML Benchmark.

Vantomex
A: 

The Expat XML Parser is also a good one. I think you will find a C++ wapper for it somewhere. http://expat.sourceforge.net/

frast
A: 

A simple google search found me quite a few XML parsers for C++ other than Xerces (the most commonly used one).

I don't know how good they are. Here are two:

libXML

TinyXML

CashCow
A: 

The boost.property_tree has the XML parser (based on RapidXml).

It's header-only and has no dependencies other than Boost.

Abyx
A: 

I've used xerces and expat (both C libraries) from within C++. Both do their job well, but if I was to rewrite my code again I'd probably use libxml2. Xerces IMO is too large and overwhelming for my needs, while expat is too low-level. My understanding is that libxml2 is a good higher level library. (I've used the lxml python bindings for libxml2 extensively - and they work beautifully.).

Oh and I've used tinyxml a bit too (several years ago), but found its DOM model too slow for my needs.

Michael Anderson
+2  A: 

I suggest pugixml. Pugixml is Light-weight, simple and fast XML parser for C++ with XPath support

Here is the quick start guide, and here is a benchmark, notice how only AsmXML is faster than pugixml.

Cristian Adam
A: 

Before you settle on an XML parser, consider first whether it is a real XML parser or is only capable of handling a subset of XML. Many parsers mentioned in the answers (like TinyXML), are not, in fact, XML 1.0 parsers. See this post for details.

There is also another approach that you may want to consider, called XML data binding, which would allow you to use XML without actually doing any XML parsing or serialization. See, for example, CodeSynthesis XSD and, for a lighter-weight, dependency-free version, CodeSynthesis XSD/e.

Boris Kolpackov