如何将 XML 字符串解析为具有状态的对象?
How do I parse XML String as Objects with states?
我有以下代码:
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null){
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
Output:
"<?xml version="1.0"?><query id="19247955" amount="10" points="44"></query><query id="19247830" amount="3" points="44"></query>"
我想将其解析为具有Id、Amount和Points等状态的对象。
我尝试使用 "DOM and a StringReader",但标签是空的,所以我失败了。
您可以尝试 java xml 绑定 (jaxb) api (http://www.mkyong.com/java/jaxb-hello-world-example/ ), or another library that also works well is beanio (http://beanio.org/).
最好只使用一次(可能更多)而不是自己动手,它们旨在从 xml 文件创建 java 对象(即解组或反序列化)和来自 java 对象的 xml 文件(即编组/序列化)。
如果您努力应用自然产生的解决方案,编程会更有趣...
编码愉快
我有以下代码:
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null){
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
Output:
"<?xml version="1.0"?><query id="19247955" amount="10" points="44"></query><query id="19247830" amount="3" points="44"></query>"
我想将其解析为具有Id、Amount和Points等状态的对象。 我尝试使用 "DOM and a StringReader",但标签是空的,所以我失败了。
您可以尝试 java xml 绑定 (jaxb) api (http://www.mkyong.com/java/jaxb-hello-world-example/ ), or another library that also works well is beanio (http://beanio.org/).
最好只使用一次(可能更多)而不是自己动手,它们旨在从 xml 文件创建 java 对象(即解组或反序列化)和来自 java 对象的 xml 文件(即编组/序列化)。 如果您努力应用自然产生的解决方案,编程会更有趣...
编码愉快