您好,登錄后才能下訂單哦!
本篇內容主要講解“c++如何使用tinyxml構建基本的SVG圖形”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“c++如何使用tinyxml構建基本的SVG圖形”吧!
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 <iostream> #include<string> #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圖形”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。