c++如何使用tinyxml构建基本的SVG图形
本篇内容主要讲解“c++如何使用tinyxml构建基本的SVG图形”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“c++如何使用tinyxml构建基本的SVG图形”吧!
成都创新互联专注于中大型企业的成都网站设计、网站制作和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户近千家,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注成都品牌网站建设和互联网程序开发,在前进的路上,与客户一起成长!
Clion的目录
CMakelists.txt
cmake_minimum_required(VERSION 3.16) project(untitled) set(CMAKE_CXX_STANDARD 14) include_directories(${CMAKE_SOURCE_DIR}) add_executable(untitled main.cpp tinystr.cpp tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp )
代码
#include#include #include "tinyxml.h" using namespace std; void createCircleSVG(TiXmlDocument &xml_doc) { // 添加XML声明 xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "GBK", "")); // 添加根元素 TiXmlElement *xml_html = new TiXmlElement("html"); xml_doc.LinkEndChild(xml_html); // 嵌套子元素 TiXmlElement *xml_body = new TiXmlElement("body"); xml_html->LinkEndChild(xml_body); TiXmlElement *xml_h2 = new TiXmlElement("h2"); xml_h2->LinkEndChild(new TiXmlText("My first SVG")); xml_body->LinkEndChild(xml_h2); TiXmlElement *xml_circle = new TiXmlElement("circle"); xml_circle->SetAttribute("cx", "100"); xml_circle->SetAttribute("cy", "50"); xml_circle->SetAttribute("r", "40"); xml_circle->SetAttribute("stroke", "black"); xml_circle->SetAttribute("stroke-width", "2"); xml_circle->SetAttribute("fill", "red"); TiXmlElement *xml_svg = new TiXmlElement("svg"); xml_svg->SetAttribute("xmlns", "http://www.w3.org/2000/svg"); xml_svg->SetAttribute("version", "1.1"); xml_svg->LinkEndChild(xml_circle); xml_body->LinkEndChild(xml_svg); } void createEllipseSVG(TiXmlDocument &xml_doc) { // 添加XML声明 xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "GBK", "")); // 添加根元素 TiXmlElement *xml_html = new TiXmlElement("html"); xml_doc.LinkEndChild(xml_html); // 嵌套子元素 TiXmlElement *xml_body = new TiXmlElement("body"); xml_html->LinkEndChild(xml_body); TiXmlElement *xml_h2 = new TiXmlElement("h2"); xml_h2->LinkEndChild(new TiXmlText("My first SVG")); xml_body->LinkEndChild(xml_h2); TiXmlElement *xml_ellipse = new TiXmlElement("ellipse"); xml_ellipse->SetAttribute("cx", "300"); xml_ellipse->SetAttribute("cy", "80"); xml_ellipse->SetAttribute("rx", "100"); xml_ellipse->SetAttribute("ry", "50"); xml_ellipse->SetAttribute("stroke", "black"); xml_ellipse->SetAttribute("stroke-width", "2"); xml_ellipse->SetAttribute("fill", "red"); TiXmlElement *xml_svg = new TiXmlElement("svg"); xml_svg->SetAttribute("xmlns", "http://www.w3.org/2000/svg"); xml_svg->SetAttribute("version", "1.1"); xml_svg->LinkEndChild(xml_ellipse); xml_body->LinkEndChild(xml_svg); } void createRectSVG(TiXmlDocument &xml_doc) { // 添加XML声明 xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "GBK", "")); // 添加根元素 TiXmlElement *xml_html = new TiXmlElement("html"); xml_doc.LinkEndChild(xml_html); // 嵌套子元素 TiXmlElement *xml_body = new TiXmlElement("body"); xml_html->LinkEndChild(xml_body); TiXmlElement *xml_h2 = new TiXmlElement("h2"); xml_h2->LinkEndChild(new TiXmlText("My first SVG")); xml_body->LinkEndChild(xml_h2); TiXmlElement *xml_rect = new TiXmlElement("rect"); xml_rect->SetAttribute("width", "300"); xml_rect->SetAttribute("height", "100"); xml_rect->SetAttribute("stroke", "black"); xml_rect->SetAttribute("stroke-width", "2"); xml_rect->SetAttribute("fill", "green"); TiXmlElement *xml_svg = new TiXmlElement("svg"); xml_svg->SetAttribute("xmlns", "http://www.w3.org/2000/svg"); xml_svg->SetAttribute("version", "1.1"); xml_svg->LinkEndChild(xml_rect); xml_body->LinkEndChild(xml_svg); } void createPolygonSVG(TiXmlDocument &xml_doc) { // 添加XML声明 xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "GBK", "")); // 添加根元素 TiXmlElement *xml_html = new TiXmlElement("html"); xml_doc.LinkEndChild(xml_html); // 嵌套子元素 TiXmlElement *xml_body = new TiXmlElement("body"); xml_html->LinkEndChild(xml_body); TiXmlElement *xml_h2 = new TiXmlElement("h2"); xml_h2->LinkEndChild(new TiXmlText("My first SVG")); xml_body->LinkEndChild(xml_h2); TiXmlElement *xml_polygon = new TiXmlElement("polygon"); xml_polygon->SetAttribute("points", "200,10 250,190 160,210"); xml_polygon->SetAttribute("stroke", "black"); xml_polygon->SetAttribute("stroke-width", "2"); xml_polygon->SetAttribute("fill", "red"); TiXmlElement *xml_svg = new TiXmlElement("svg"); xml_svg->SetAttribute("xmlns", "http://www.w3.org/2000/svg"); xml_svg->SetAttribute("version", "1.1"); xml_svg->LinkEndChild(xml_polygon); xml_body->LinkEndChild(xml_svg); } void createLineSVG(TiXmlDocument &xml_doc) { // 添加XML声明 xml_doc.LinkEndChild(new TiXmlDeclaration("1.0", "GBK", "")); // 添加根元素 TiXmlElement *xml_html = new TiXmlElement("html"); xml_doc.LinkEndChild(xml_html); // 嵌套子元素 TiXmlElement *xml_body = new TiXmlElement("body"); xml_html->LinkEndChild(xml_body); TiXmlElement *xml_h2 = new TiXmlElement("h2"); xml_h2->LinkEndChild(new TiXmlText("My first SVG")); xml_body->LinkEndChild(xml_h2); TiXmlElement *xml_line = new TiXmlElement("line"); xml_line->SetAttribute("x1", "100"); xml_line->SetAttribute("y1", "100"); xml_line->SetAttribute("x2", "400"); xml_line->SetAttribute("y2", "400"); xml_line->SetAttribute("stroke", "black"); xml_line->SetAttribute("stroke-width", "2"); xml_line->SetAttribute("fill", "green"); TiXmlElement *xml_svg = new TiXmlElement("svg"); xml_svg->SetAttribute("xmlns", "http://www.w3.org/2000/svg"); xml_svg->SetAttribute("version", "1.1"); xml_svg->LinkEndChild(xml_line); xml_body->LinkEndChild(xml_svg); } void parseCircleSVG(TiXmlDocument &xml_doc) { TiXmlHandle docHandle(&xml_doc); TiXmlElement *child = docHandle.FirstChild("html").FirstChild("body").FirstChild("svg").ToElement(); for (child; child; child = child->NextSiblingElement()) { string xmlnsStr = child->Attribute("xmlns"); string versionStr = child->Attribute("version"); std::cout << "xmlns :" << xmlnsStr << " version" << versionStr << std::endl; TiXmlElement *circleItem = child->FirstChild()->ToElement(); string cxStr = circleItem->Attribute("cx"); string cyStr = circleItem->Attribute("cy"); string rStr = circleItem->Attribute("r"); string strokeStr = circleItem->Attribute("stroke"); string stroke_widthStr = circleItem->Attribute("stroke-width"); string fillStr = circleItem->Attribute("fill"); std::cout << " cx :" << cxStr << " cy :" << cyStr << " r :" << rStr << " stroke :" << strokeStr << " stroke-width :" << stroke_widthStr << " fill :" << fillStr << std::endl; } } void parseRectSVG(TiXmlDocument &xml_doc) { TiXmlHandle docHandle(&xml_doc); TiXmlElement *child = docHandle.FirstChild("html").FirstChild("body").FirstChild("svg").ToElement(); for (child; child; child = child->NextSiblingElement()) { string xmlnsStr = child->Attribute("xmlns"); string versionStr = child->Attribute("version"); std::cout << "xmlns :" << xmlnsStr << " version" << versionStr << std::endl; TiXmlElement *circleItem = child->FirstChild()->ToElement(); string widthStr = circleItem->Attribute("width"); string heightStr = circleItem->Attribute("height"); string strokeStr = circleItem->Attribute("stroke"); string stroke_widthStr = circleItem->Attribute("stroke-width"); string fillStr = circleItem->Attribute("fill"); std::cout << " width :" << widthStr << " height :" << heightStr << " stroke :" << strokeStr << " stroke-width :" << stroke_widthStr << " fill :" << fillStr << std::endl; } } void parseEllipseSVG(TiXmlDocument &xml_doc) { TiXmlHandle docHandle(&xml_doc); TiXmlElement *child = docHandle.FirstChild("html").FirstChild("body").FirstChild("svg").ToElement(); for (child; child; child = child->NextSiblingElement()) { string xmlnsStr = child->Attribute("xmlns"); string versionStr = child->Attribute("version"); std::cout << "xmlns :" << xmlnsStr << " version" << versionStr << std::endl; TiXmlElement *circleItem = child->FirstChild()->ToElement(); string cxStr = circleItem->Attribute("cx"); string cyStr = circleItem->Attribute("cy"); string rxStr = circleItem->Attribute("rx"); string ryStr = circleItem->Attribute("ry"); string strokeStr = circleItem->Attribute("stroke"); string stroke_widthStr = circleItem->Attribute("stroke-width"); string fillStr = circleItem->Attribute("fill"); std::cout << " cx :" << cxStr << " cy :" << cyStr << " rx :" << rxStr << " ry :" << ryStr << " stroke :" << strokeStr << " stroke-width :" << stroke_widthStr << " fill :" << fillStr << std::endl; } } void parsePolygonSVG(TiXmlDocument &xml_doc) { TiXmlHandle docHandle(&xml_doc); TiXmlElement *child = docHandle.FirstChild("html").FirstChild("body").FirstChild("svg").ToElement(); for (child; child; child = child->NextSiblingElement()) { string xmlnsStr = child->Attribute("xmlns"); string versionStr = child->Attribute("version"); std::cout << "xmlns :" << xmlnsStr << " version" << versionStr << std::endl; TiXmlElement *circleItem = child->FirstChild()->ToElement(); string pointsStr = circleItem->Attribute("points"); string strokeStr = circleItem->Attribute("stroke"); string stroke_widthStr = circleItem->Attribute("stroke-width"); string fillStr = circleItem->Attribute("fill"); std::cout << " points :" << pointsStr << " stroke :" << strokeStr << " stroke-width :" << stroke_widthStr << " fill :" << fillStr << std::endl; } } void parseLineSVG(TiXmlDocument &xml_doc) { TiXmlHandle docHandle(&xml_doc); TiXmlElement *child = docHandle.FirstChild("html").FirstChild("body").FirstChild("svg").ToElement(); for (child; child; child = child->NextSiblingElement()) { string xmlnsStr = child->Attribute("xmlns"); string versionStr = child->Attribute("version"); std::cout << "xmlns :" << xmlnsStr << " version" << versionStr << std::endl; TiXmlElement *circleItem = child->FirstChild()->ToElement(); string x1Str = circleItem->Attribute("x1"); string y1Str = circleItem->Attribute("y1"); string x2Str = circleItem->Attribute("x2"); string y2Str = circleItem->Attribute("y2"); string strokeStr = circleItem->Attribute("stroke"); string stroke_widthStr = circleItem->Attribute("stroke-width"); string fillStr = circleItem->Attribute("fill"); std::cout << " x1 :" << x1Str << " y1 :" << y1Str << " x2 :" << x2Str << " y2 :" << y2Str << " stroke :" << strokeStr << " stroke-width :" << stroke_widthStr << " fill :" << fillStr << std::endl; } } void constructCircle() { TiXmlDocument xml_doc_Circle; std::string exampleCircle = "circle.html"; // 可以改成xml后缀 createCircleSVG(xml_doc_Circle); xml_doc_Circle.SaveFile(exampleCircle.c_str()); // if(!xml_doc.LoadFile(example.c_str())) // { // cerr << xml_doc.ErrorDesc() << endl; // return -1; // } // 也可以读取文件 进行解析 parseCircleSVG(xml_doc_Circle); } void constructRect() { TiXmlDocument xml_doc_Rect; std::string exampleRect = "rect.html"; // 可以改成xml后缀 createRectSVG(xml_doc_Rect); // 保存到文件 xml_doc_Rect.SaveFile(exampleRect.c_str()); parseRectSVG(xml_doc_Rect); } void constructEllipse() { TiXmlDocument xml_doc_Ellipse; std::string exampleEllipse = "Ellipse.html"; // 可以改成xml后缀 createEllipseSVG(xml_doc_Ellipse); xml_doc_Ellipse.SaveFile(exampleEllipse.c_str()); parseEllipseSVG(xml_doc_Ellipse); } void constructPolygon() { TiXmlDocument xml_doc_Polygon; std::string examplePolygon = "Polygon.html"; // 可以改成xml后缀 createPolygonSVG(xml_doc_Polygon); xml_doc_Polygon.SaveFile(examplePolygon.c_str()); parsePolygonSVG(xml_doc_Polygon); } void constructLine() { TiXmlDocument xml_doc_Line; std::string exampleLine = "Line.html"; // 可以改成xml后缀 createLineSVG(xml_doc_Line); xml_doc_Line.SaveFile(exampleLine.c_str()); parseLineSVG(xml_doc_Line); } int main() { constructCircle(); constructRect(); constructEllipse(); constructPolygon(); constructLine(); std::cout << "xml generate accomplished!\n"; return 0; }
测试结果
F:\untitled\cmake-build-debug\untitled.exe xmlns :http://www.w3.org/2000/svg version1.1 cx :100 cy :50 r :40 stroke :black stroke-width :2 fill :red xmlns :http://www.w3.org/2000/svg version1.1 width :300 height :100 stroke :black stroke-width :2 fill :green xmlns :http://www.w3.org/2000/svg version1.1 cx :300 cy :80 rx :100 ry :50 stroke :black stroke-width :2 fill :red xmlns :http://www.w3.org/2000/svg version1.1 points :200,10 250,190 160,210 stroke :black stroke-width :2 fill :red xmlns :http://www.w3.org/2000/svg version1.1 x1 :100 y1 :100 x2 :400 y2 :400 stroke :black stroke-width :2 fill :green xml generate accomplished! Process finished with exit code 0
到此,相信大家对“c++如何使用tinyxml构建基本的SVG图形”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分享文章:c++如何使用tinyxml构建基本的SVG图形
网站路径:http://ybzwz.com/article/gghjep.html