How to resolve the algorithm XML/Output step by step in the C++ programming language
How to resolve the algorithm XML/Output step by step in the C++ programming language
Table of Contents
Problem Statement
Create a function that takes a list of character names and a list of corresponding remarks and returns an XML document of
Let's start with the solution:
Step by Step solution about How to resolve the algorithm XML/Output step by step in the C++ programming language
The code starts by including some header files, including <vector>
, <utility>
, <iostream>
, and <boost/algorithm/string.hpp>
.
The create_xml()
function is defined, which takes two vectors of strings as parameters, names
and remarks
.
The main() function creates two vectors of strings, names
and remarks
, and pushes some data into them.
The create_xml()
function is then called with the names
and remarks
vectors as arguments, and the resulting XML string is printed to the console.
The create_xml()
function first creates a vector of pairs of strings, entities
, which maps HTML entities to their corresponding XML entities.
The function then loops through the names
vector and replaces any occurrences of the HTML entities with their corresponding XML entities. The same is done for the remarks
vector.
Finally, the function loops through the names
vector and creates an XML string for each name and its corresponding remark. The XML string is returned at the end of the function.
Source code in the cpp programming language
#include <vector>
#include <utility>
#include <iostream>
#include <boost/algorithm/string.hpp>
std::string create_xml( std::vector<std::string> & ,std::vector<std::string> & ) ;
int main( ) {
std::vector<std::string> names , remarks ;
names.push_back( "April" ) ;
names.push_back( "Tam O'Shantor" ) ;
names.push_back ( "Emily" ) ;
remarks.push_back( "Bubbly, I'm > Tam and <= Emily" ) ;
remarks.push_back( "Burns: \"When chapman billies leave the street ...\"" ) ;
remarks.push_back( "Short & shrift" ) ;
std::cout << "This is in XML:\n" ;
std::cout << create_xml( names , remarks ) << std::endl ;
return 0 ;
}
std::string create_xml( std::vector<std::string> & names ,
std::vector<std::string> & remarks ) {
std::vector<std::pair<std::string , std::string> > entities ;
entities.push_back( std::make_pair( "&" , "&" ) ) ;
entities.push_back( std::make_pair( "<" , "<" ) ) ;
entities.push_back( std::make_pair( ">" , ">" ) ) ;
std::string xmlstring ( "<CharacterRemarks>\n" ) ;
std::vector<std::string>::iterator vsi = names.begin( ) ;
typedef std::vector<std::pair<std::string , std::string> >::iterator Vpss ;
for ( ; vsi != names.end( ) ; vsi++ ) {
for ( Vpss vs = entities.begin( ) ; vs != entities.end( ) ; vs++ ) {
boost::replace_all ( *vsi , vs->first , vs->second ) ;
}
}
for ( vsi = remarks.begin( ) ; vsi != remarks.end( ) ; vsi++ ) {
for ( Vpss vs = entities.begin( ) ; vs != entities.end( ) ; vs++ ) {
boost::replace_all ( *vsi , vs->first , vs->second ) ;
}
}
for ( int i = 0 ; i < names.size( ) ; i++ ) {
xmlstring.append( "\t<Character name=\"").append( names[ i ] ).append( "\">")
.append( remarks[ i ] ).append( "</Character>\n" ) ;
}
xmlstring.append( "</CharacterRemarks>" ) ;
return xmlstring ;
}
You may also check:How to resolve the algorithm Digital root step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Array length step by step in the Beef programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Groovy programming language
You may also check:How to resolve the algorithm Comments step by step in the Blast programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Wrapl programming language