Parsing a file with multiple xmls in it

前端 未结 4 1029
半阙折子戏
半阙折子戏 2021-01-15 11:43

Is there a way to parse a file which contains multiple xmls in it?

eg., if I have a file called stocks.xml and within the stocks.xml i have more than one xml content

4条回答
  •  隐瞒了意图╮
    2021-01-15 12:24

    If you can assume that each xml document begins with , simply read the file line-by-line looking for a lines that match that pattern (or, read all the data and then do a search through the data).

    Once you find a line, keep it, and append subsequent lines until the next xml document is found or you hit EOF. lather, rinse, repeat.

    You now have one xml document in a string. You can then parse the string using the normal XML parsing tools, or you write it to a file.

    This will work fine in most cases, but of course it could fall down if one of your embedded xml documents contains data that exactly matches the same pattern as the beginning of a document. Most likely you don't have to worry about that, and if you do there are ways to avoid that with a little more cleverness.

    The right solution really depends on your needs. If you're creating a general purpose must-work-at-all-times solution this might not be right for you. For real world, special purpose problems it's probably more than Good Enough, and often Good Enough is indeed Good Enough.

提交回复
热议问题