How to read a string line by line in C++
I have a string with an xml code in it. I want to read from it line by line so i can extract the strings betweens "title" tags.
I know how to extract the titles, but how 开发者_如何转开发do i traverse the string ? Sounds easy but i have no idee right now. Thanks in advanced.Maybe you can give some more details about what extracting the strings between the "title" tags means?
If you already can extract the title tags, then that means you know their positions, so then extracting the string is just a matter of taking the substring between the opening and closing title tags right?
Are you looking for a XML parser? The opensource libxml works well, and has bindings for a variety of languages. There are other parsers, what parsers allow you to do is to take the XML string and create a tree data structure which gives you easy access to the elements of the XML.
EDIT: Originally the requirement about not using an xml parser didn't exist in the question. Here's a rough algorithm to create your own XML parser.
1) Create a tree data structure, and a recursive parse() function. 2) Search for a XML tag, anything with the pattern <...>. Add the "..." tag to one of the child nodes of the current node you are on, and call the recursive parse() function again. 3) If you find a XML tag that closes the orginal <...>, then you are done with parsing that block. Go back to step #2. If there are no other blocks then return from the parse function.
Here's some pseudo code:
// node: The current node in the tree
// current_position: the current position in the XML string that you are parsing
// string: the XML string that you are parsing.
parse(node, current_position, string):
while current_position < len(string):
current_position = find(string[current_position:len(string)], "<...>")
if !found: return current_position // should be end of string if nothing is found.
node.children[node.num_children] = new Node("<...>");
current_position = parse(node.children[node.num_children],current_position+size_of_tag,string)
current_position = find(string[current_position:len(string)], "</...>")
node.num_children++
return current_position
精彩评论