From be1ad4b85501b261e25eed6bbb0dd557d3b45018 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Tue, 14 May 2019 11:21:10 +0200 Subject: [PATCH 1/2] Stream_support: Show how to read an xml file using boost::property_tree --- .../doc/Stream_support/IOstream.txt | 18 ++++++++++ .../doc/Stream_support/examples.txt | 1 + .../examples/Stream_support/CMakeLists.txt | 2 ++ .../examples/Stream_support/data/cloud.pol | 8 +++++ .../examples/Stream_support/read_xml.cpp | 35 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 Stream_support/examples/Stream_support/data/cloud.pol create mode 100644 Stream_support/examples/Stream_support/read_xml.cpp diff --git a/Stream_support/doc/Stream_support/IOstream.txt b/Stream_support/doc/Stream_support/IOstream.txt index b801055ad88..fb6e4c16b59 100644 --- a/Stream_support/doc/Stream_support/IOstream.txt +++ b/Stream_support/doc/Stream_support/IOstream.txt @@ -258,6 +258,24 @@ in those WKT types, namely: - `CGAL::Point_3` - `CGAL::Polygon_with_holes_2` - random access range of the above types. + +\section IOstreamXML Reading Adhoc XML + +In case you have to read data in a simple xml file with a simple structure, without existing I/O libraries +the `boost::property_tree` comes in handy. +The following small example shows how to parse a file +looking like this: + + + + + + + + + + +\cgalExample{Stream_support/read_xml.cpp} */ } /* namespace CGAL */ diff --git a/Stream_support/doc/Stream_support/examples.txt b/Stream_support/doc/Stream_support/examples.txt index 25891e95051..b8fd39550b6 100644 --- a/Stream_support/doc/Stream_support/examples.txt +++ b/Stream_support/doc/Stream_support/examples.txt @@ -2,4 +2,5 @@ \example Stream_support/Linestring_WKT.cpp \example Stream_support/Point_WKT.cpp \example Stream_support/Polygon_WKT.cpp +\example Stream_support/read_xml.cpp */ diff --git a/Stream_support/examples/Stream_support/CMakeLists.txt b/Stream_support/examples/Stream_support/CMakeLists.txt index 165c4ae8cec..84b853a2b66 100644 --- a/Stream_support/examples/Stream_support/CMakeLists.txt +++ b/Stream_support/examples/Stream_support/CMakeLists.txt @@ -35,6 +35,8 @@ if ( CGAL_FOUND ) create_single_source_cgal_program( "Polygon_WKT.cpp" ) create_single_source_cgal_program( "Linestring_WKT.cpp" ) create_single_source_cgal_program( "read_WKT.cpp" ) + + create_single_source_cgal_program( "read_xml.cpp" ) else () message(STATUS "This project requires the CGAL library, and will not be compiled.") diff --git a/Stream_support/examples/Stream_support/data/cloud.pol b/Stream_support/examples/Stream_support/data/cloud.pol new file mode 100644 index 00000000000..c3861de634b --- /dev/null +++ b/Stream_support/examples/Stream_support/data/cloud.pol @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/Stream_support/examples/Stream_support/read_xml.cpp b/Stream_support/examples/Stream_support/read_xml.cpp new file mode 100644 index 00000000000..fdbbf4b0ec3 --- /dev/null +++ b/Stream_support/examples/Stream_support/read_xml.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef K::Point_3 Point_3; + +int main(int argc, char* argv[]){ + std::ifstream in( (argc>1)? argv[1] : "data/cloud.pol"); + boost::property_tree::ptree tree; + boost::property_tree::read_xml(in, tree); + + std::vector points; + + for(boost::property_tree::ptree::value_type& node : tree.get_child("PolySet.Polygon")){ + boost::property_tree::ptree subtree = node.second; + if( node.first == "Point" ){ + for( boost::property_tree::ptree::value_type const& v : subtree.get_child( "" ) ) { + std::string label = v.first; + + if ( label == "" ) { + Point_3 p(subtree.get( label+".X"), + subtree.get( label+".Y"), + subtree.get( label+".Z")); + points.push_back(p); + } + } + } + } + + std::cout << points.size() << " points read"<< std::endl; + return 0; +} From 78c5ec760f982d936f2a22fc0926a921c76b3546 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 24 May 2019 14:20:50 +0200 Subject: [PATCH 2/2] Fix the URL --- Stream_support/doc/Stream_support/IOstream.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stream_support/doc/Stream_support/IOstream.txt b/Stream_support/doc/Stream_support/IOstream.txt index fb6e4c16b59..d651f21ac55 100644 --- a/Stream_support/doc/Stream_support/IOstream.txt +++ b/Stream_support/doc/Stream_support/IOstream.txt @@ -262,7 +262,7 @@ in those WKT types, namely: \section IOstreamXML Reading Adhoc XML In case you have to read data in a simple xml file with a simple structure, without existing I/O libraries -the `boost::property_tree` comes in handy. +the `boost::property_tree` comes in handy. The following small example shows how to parse a file looking like this: