【发布时间】:2016-01-27 17:35:06
【问题描述】:
来自服务器的响应是这样的:
<oob>
<type>screen</type>
<value>idle</value>
<action>show</action>
</oob>
<oob>
<type>schedule</type>
<action>show</action>
</oob>
我想将所有标签作为键和值放在标签内作为值。标签数量和标签类型未知。我想要这样的东西:
//for first string from server
public HashMap<String, String> response = new HashMap<String, String>();
response.put("type","screen");
response.put("value","idle");
response.put("action","show");
//for second string
response.put("type","schedule");
response.put("action","show");
应该有解析字符串的逻辑:
if(server_response.contains("<oob>")){
while(!endof server_response)
response.put("?","?");
}
如何解析该格式的服务器响应?
【问题讨论】:
-
一个 XML 解析器。也许是旧的 DOM,也许用 XPath 将它分开,也许使用 JAXB,但基本上是 Java XML 解析。
-
你真的得到格式错误的 XML(在这种情况下,没有单个根元素)吗?
标签: java string xml-parsing