简体   繁体   English

如何在C ++中逐行读取字符串

[英]How to read a string line by line in C++

I have a string with an xml code in it. 我有一个带有xml代码的字符串。 I want to read from it line by line so i can extract the strings betweens "title" tags. 我想逐行阅读,以便提取“ title”标签之间的字符串。
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? 也许您可以提供更多有关提取“ title”标签之间的字符串的含义的详细信息?

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? 您是否正在寻找XML解析器? The opensource libxml works well, and has bindings for a variety of languages. 开源libxml运作良好,并且具有多种语言的绑定。 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. 还有其他解析器,解析器允许您执行的操作是获取XML字符串并创建树数据结构,该结构使您可以轻松访问XML的元素。

EDIT: Originally the requirement about not using an xml parser didn't exist in the question. 编辑:最初关于不使用xml解析器的要求在问题中不存在。 Here's a rough algorithm to create your own XML parser. 这是创建您自己的XML解析器的粗略算法。

1) Create a tree data structure, and a recursive parse() function. 1)创建一个树数据结构,以及一个递归的parse()函数。 2) Search for a XML tag, anything with the pattern <...>. 2)搜索XML标记,任何带有模式<...>的内容。 Add the "..." tag to one of the child nodes of the current node you are on, and call the recursive parse() function again. 将“ ...”标记添加到当前所在节点的子节点之一,然后再次调用递归parse()函数。 3) If you find a XML tag that closes the orginal <...>, then you are done with parsing that block. 3)如果找到关闭原始<...>的XML标记,则解析该块已完成。 Go back to step #2. 返回步骤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           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM