英文:
Get Json/Xml out of larger text
问题
我有一个大文本,内容如下:
一些文本 blabla
<a><b attr="/>!" /></a>
其他一些文本 ...
{ "field": "value", "inner": { "a":true, "b": false} }
还有更多文本
在这个文本中,有几个有效的 XML 和 JSON 部分。我想通过传递我想要的部分的起始索引来获取正确的子字符串。像这样:
String text = ..; // 来自上面的文本
String xml1 = util.getXml(text, 17); // 17 表示从 "<a ..." 处开始搜索
// 期望的 xml1: <a><b attr="/>!" /></a>
String text = "..";
String xml2 = util.getXml(text, 20); // 20 表示从 "<b at...." 处开始搜索
// 期望的 xml2: <b attr="/>!" />
String text = "..";
String json1 = util.getJson(text, 61); // 61 表示从 "{ "fi..." 处开始搜索
// 期望的 json1: { "field": "value", "inner": { "a":true, "b": false} }
我猜这是一个非常特殊的情况,因为我找不到任何相关资料。
我在考虑使用正则表达式和 XML/JSON 流解析器来自己实现,但我想首先询问一下是否可能有任何解决方案存在。
英文:
I'm having a large text which looks like this
Some text blabla
<a><b attr="/>!" /></a>
Some other text ...
{ "field": "value", "inner": { "a":true, "b": false} }
Ans some more text
Within this text there are several valid xml and json parts. I would like to get the correct substring by passing the start index of the part I want. Like this:
String text = ..; // from above
String xml1 = util.getXml(text, 17); // 17 means to start search at "<a ..."
// expected xml1: <a><b attr="/>!" /></a>
String text = "..";
String xml2 = util.getXml(text, 20); // 20 means to start search at "<b at...."
// expected xml2: <b attr="/>!" />
String text = "..";
String json1 = util.getJson(text, 61); // 61 means to start search at "{ "fi..."
// expected json1: { "field": "value", "inner": { "a":true, "b": false} }
I guess it's a very special case because I didn't found anything related.
I'm thinking about regex and xml/json stream parsers to implement it on my own but I wanted to ask first if there is possibly any solution out there.
专注分享java语言的经验与见解,让所有开发者获益!
评论