我似乎无法从下面的xml中解析出值。如果我使用的是google-http-java客户端,那么解析类型的xml的解析器类是什么?下面是示例xml和解析器
<feed>
<text start="1" end="10"> Text 1 </text>
<text start="2" end="20"> Text 2 </text>
<text start="3" end="30"> Text 3 </text>
<text start="4" end="40"> Text 4 </text>
<text start="5" end="50"> Text 5 </text>
</feed>
Class Feed {
@Key("text")
public List<Text> textList;
}
Class Text {
@Key("@start")
public String startTime;
@Key("@end")
public String endTime;
}
为了清楚起见,我需要start属性值、end属性值和文本内容。看起来有效的方法如下
HttpRequestFactory httpRequestFactory =
HTTP_TRANSPORT.createRequestFactory(
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setParser(new XmlObjectParser(new XmlNamespaceDictionary().set("", "")));
}
});
Feed feedObject = httpResponse.parseAs(Feed.class);
但我无法获取内容值。
如果我将提要类更改为以下内容
Class Feed {
@Key("text")
public List<String> textList;
}
我只能获取内容,而不能获取属性值!
任何示例源代码都很难找到(在github、stackoverflow等上)
发布于 2017-03-26 00:38:13
好的,这是通过github梳理后的答案!
<feed>
<text start="1" end="10"> Text 1 </text>
<text start="2" end="20"> Text 2 </text>
<text start="3" end="30"> Text 3 </text>
<text start="4" end="40"> Text 4 </text>
<text start="5" end="50"> Text 5 </text>
</feed>
Class Feed {
@Key("text")
public List<Text> textList;
}
Class Text {
@Key("@start")
public String startTime;
@Key("@end")
public String endTime;
@Key("text()")
public String payload;
}
https://stackoverflow.com/questions/42947321
复制