Examples

These simple examples gives you the way to start with libgexf.

Creating content

How to add nodes, edges, node labels, data attributes and meta data:

http://gexf.net/data/create.cpp
#include <stdlib.h>
// if libgexf is installed
#include <libgexf/libgexf.h>

void create() {
    libgexf::GEXF *gexf = new libgexf::GEXF();
    libgexf::DirectedGraph& graph = gexf->getDirectedGraph();

    // nodes
    graph.addNode("0");
    graph.addNode("1");

    // edges
    graph.addEdge("0", "0", "1");

    // node labels
    libgexf::Data& data = gexf->getData();
    data.setLabel("0", "Hello");
    data.setLabel("1", "world");

    // attributes
    data.addNodeAttributeColumn("0", "foo", libgexf::BOOLEAN);
    data.setNodeAttributeDefault("0", "false");
    data.setNodeValue("1", "0", "true");

    // meta data
    libgexf::MetaData& meta = gexf->getMetaData();
    meta.setCreator("The Hitchhiker's Guide to the Galaxy");
    meta.setDescription("The answer is 42.");

    // dump
    std::cout << "Dump:" << std::endl << *gexf;
}

Reading a GEXF file

Simply read the in.gexf file and load it in memory:

http://gexf.net/data/read.cpp
#include <stdlib.h>
// if libgexf is installed
#include <libgexf/libgexf.h>

libgexf::GEXF read() {
    libgexf::FileReader* reader = new libgexf::FileReader();
    reader->init("in.gexf");
    reader->slurp();

    return reader->getGEXFCopy();
}

Writing a GEXF file

Simply read the in.gexf file and load it in memory:

http://gexf.net/data/write.cpp
#include <stdlib.h>
// if libgexf is installed
#include <libgexf/libgexf.h>

void write(libgexf::GEXF *gexf) {
    libgexf::FileWriter* writer = new libgexf::FileWriter();
    writer->init("out.gexf", gexf);
    writer->write();
}

XSD validation

How to validate a GEXF file regarding the XML Schema definition:

http://gexf.net/data/xsd_validate.cpp
#include <stdlib.h>
// if libgexf is installed
#include <libgexf/libgexf.h>

void validate() {
    bool res = libgexf::SchemaValidator::run( "path/to/my/file.gexf",
                                              "/usr/include/libgexf/resources/xsd/gexf.xsd");
    std::cout << "isValid: " << res << std::endl;
}

Data integrity and type checking

How to check node labels, the types of attribute data, default values of liststrings, and if each attvalue has a value or a defaultvalue:

bool ok = gexf.checkIntegrity();

Go back >> home