A lab job for classmates: separate the title and content of the XML.
Stack
#include <iostream> #include <stack> using namespace std;
int main () {string str;
stack<char>tag,contents,xml;
int i,j,k,t;
Getline (CIN,STR);//input string str for (i=0; I<str.length (); i++) {if (str[i]!= '/') {Xml.push (str[i]); } else if (str[i]== '/') {while (Xml.size ()!=0) {if (xml.top () = ' > ') {contents.push (Xml.top ())
;
Xml.pop ();
} else if (xml.top () = = ' > ') {cout<< "Contents is:";
while (Contents.size ()!=1) {cout<<contents.top ();
Contents.pop ();
} contents.pop ();
cout<<endl;
Break
}} tag.push (' > ');
while (Xml.size ()!=0) {if (xml.top () = ' < ') {Tag.push (Xml.top ());
Xml.pop ();
} else if (xml.top () = = ' < ') {cout<< "Tag is:<";
while (Tag.size ()!=1) {cout<<tag.top ();
Tag.pop ();
} tag.pop ();
cout<<endl;
Break }
}
}
}
}
Input
<title>empire Burlesque</title><artist>bob Dylan</artist><country>usa</country >
Output
Tag is:<title>
Contents Is:empire Burlesque
Tag is:<artist>
Contents Is:bob Dylan
Tag is:<country>
Contents Is:usa