【问题标题】:How do I parse XML String as Objects with states?如何将 XML 字符串解析为具有状态的对象?
【发布时间】:2023-03-20 21:53:01
【问题描述】:

我有以下代码:

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 和 StringReader”,但标签是空的,所以我失败了。

【问题讨论】:

标签: java xml string parsing object


【解决方案1】:

您可以尝试 java xml binding (jaxb) api (http://www.mkyong.com/java/jaxb-hello-world-example/ ),或者另一个也可以正常工作的库是 beanio (http://beanio.org/)。

最好只使用一次(可能更多次)而不是自己处理问题,它们旨在从 xml 文件(即解组或反序列化)创建 java 对象和从 java 对象创建 xml 文件(即编组/序列化)。 如果您努力应用自然产生的解决方案,编程会更有趣......

愉快的编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多