Example of svg parsing

360 views
Skip to first unread message

Alexander Aravenkov

unread,
Oct 19, 2017, 11:04:45 AM10/19/17
to SVG++ library
Hi! Do you have some full example for parsing SVG? So what i did: created SVG file in SVG-editor, read this file using std::ifstream, added svg-text to rapidxml and tried to parse. And rapidxml create exception with text "Error loading SCG: second colon in element name". Full code of my programm and SVG text below:

Programm:

#include "stdafx.h"

#include <rapidxml_ns/rapidxml_ns.hpp>
#include <svgpp/policy/xml/rapidxml_ns.hpp>
#include <svgpp/svgpp.hpp>
#include <iostream>
#include <fstream>
#include <string>

using namespace svgpp;

typedef rapidxml_ns::xml_node<> const * xml_element_t;

class Context
{
public:
void on_enter_element(tag::element::any)
{
}

void on_exit_element()
{
}

void path_move_to(double x, double y, tag::coordinate::absolute)
{
}

void path_line_to(double x, double y, tag::coordinate::absolute)
{
}

void path_cubic_bezier_to(
double x1, double y1,
double x2, double y2,
double x, double y,
tag::coordinate::absolute)
{
}

void path_quadratic_bezier_to(
double x1, double y1,
double x, double y,
tag::coordinate::absolute)
{
}

void path_elliptical_arc_to(
double rx, double ry, double x_axis_rotation,
bool large_arc_flag, bool sweep_flag,
double x, double y,
tag::coordinate::absolute)
{
}

void path_close_subpath()
{
}

void path_exit()
{
}
};

typedef
boost::mpl::set<
// SVG Structural Elements
tag::element::svg,
tag::element::g,
// SVG Shape Elements
tag::element::circle,
tag::element::ellipse,
tag::element::line,
tag::element::path,
tag::element::polygon,
tag::element::polyline,
tag::element::rect
>::type processed_elements_t;

void loadSvg(xml_element_t xml_root_element)
{
Context context;
document_traversal<
processed_elements<processed_elements_t>,
processed_attributes<traits::shapes_attributes_by_element>
>::load_document(xml_root_element, context);
}

int main()
{
std::string svg_text;
std::ifstream file;
file.open("d:\\Temp\\drawing.svg", std::ios_base::in);
if (file.is_open())
{
while (!file.eof())
{
std::string tmp;
file >> tmp;
svg_text += tmp;
}
}

rapidxml_ns::xml_document<> doc;    // character type defaults to char
try
{
doc.parse<0>((char*)svg_text.c_str());
if (rapidxml_ns::xml_node<> * svg_element = doc.first_node("svg"))
{
loadSvg(svg_element);
}
}
catch (std::exception const & e)
{
std::cerr << "Error loading SVG: " << e.what() << std::endl;
return 1;
}

return 0;
}

SVG file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   id="svg8"
   version="1.1"
   viewBox="0 0 210 297"
   height="297mm"
   width="210mm">
  <defs
     id="defs2" />
  <metadata
     id="metadata5">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     id="layer1">
    <rect
       y="46.023808"
       x="40.82143"
       height="128.5119"
       width="124.73214"
       id="rect18"
       style="fill:#00ff00;stroke-width:0.26458332" />
    <rect
       y="126.15475"
       x="96.005951"
       height="109.61309"
       width="76.351196"
       id="rect20"
       style="fill:#0000ff;stroke-width:0.26458332" />
  </g>
</svg>



svgpp

unread,
Oct 19, 2017, 1:24:43 PM10/19/17
to SVG++ library
Hi Alexander,

Your code that reads from file concatenates lines dropping new line characters so you're getting invalid XML like <cc:Workrdf:about="">.

You may use RapidXML utils to read from file or any correct function to read file content into string.
Excerpts from svgpp/src/samples/sample_animation.cpp:

#include <rapidxml_ns/rapidxml_ns_utils.hpp>
rapidxml_ns::file<> xml_file(argv[1]);
rapidxml_ns::xml_document<> doc;
doc.parse<rapidxml_ns::parse_no_string_terminators>(xml_file.data());
Regards,
Oleg
Reply all
Reply to author
Forward
0 new messages