如何使用 XStream 将 XML 解析为 java 对象

发布于 2024-12-25 03:55:20 字数 481 浏览 0 评论 0原文

我有一些 xml 代码,我想将其解析为 java 对象,请帮助我。

   <Error>
    <number>1020</number>
    <Type>fatal</Type>
    <Text>Nagaraju</Text>
    <Text>Suresh</Text>
    <Text>Sound</Text>
    <Text>Rajesh</Text>
   </Error>

java对象是

   Class Error{
      int number;
      String type;
      List<String>texts=new ArrayList<String>();
   }

i have some xml code, and i want to parse this to java object please help me.

   <Error>
    <number>1020</number>
    <Type>fatal</Type>
    <Text>Nagaraju</Text>
    <Text>Suresh</Text>
    <Text>Sound</Text>
    <Text>Rajesh</Text>
   </Error>

java object is

   Class Error{
      int number;
      String type;
      List<String>texts=new ArrayList<String>();
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2025-01-01 03:55:20

我会亲自手动映射它,或者也许使用 JAX-B,但如果您确实想使用 XStream:

  1. 您需要将类 Error 映射到元素 Error。您可以使用“别名”来完成此操作。 http://x-stream.github.io/alias-tutorial.html
  2. 列表通常不以这种方式表示,它通常是嵌套的。将您的 Error 对象序列化为 XML 以查看它通常表示的内容。如果你想这样做,你可能需要一个转换器: http://x- Stream.github.io/converter-tutorial.html
  3. 您还可以通过注册 Error 和文本元素来使用隐式集合。 http://x-stream .github.io/javadoc/com/thoughtworks/xstream/XStream.html#addImplicitCollection(java.lang.Class, java.lang.String, java.lang.Class)

I would map it manually personally, or use JAX-B perhaps, but if you really want to use XStream:

  1. You'll need to map the class Error to the element Error. You can do this with an "alias". http://x-stream.github.io/alias-tutorial.html
  2. The List isn't usually represented that way, it would normally be nested. Serialise your Error object out to XML to see what it would normally be represented as. If you want to do this way you'll likely need a convertor: http://x-stream.github.io/converter-tutorial.html
  3. You could also use implicit collections, by registering the Error and text element. http://x-stream.github.io/javadoc/com/thoughtworks/xstream/XStream.html#addImplicitCollection(java.lang.Class, java.lang.String, java.lang.Class)
忆悲凉 2025-01-01 03:55:20

使用代码:

    String response = "<\patron_tenders><\patron_tender name=\"alpha\" city=\"mumbai\" >"
+ "<\partial_card_number>************6319<\partial_card_number>"
+ "<\zip_code>123456<\zip_code>"
+ "<\tender_type_name>Visa<\tender_type_name>"
+ "<\token_id>80195<\token_id>" + "<\expired>false<\expired>"
+ "<\patron_tender>"+"<\patron_tender name=\"beta\" city=\"pune\" >"
+ "<\partial_card_number>************3545<\partial_card_number>"
+ "<\zip_code>565768<\zip_code>"
+ "<\tender_type_name>Visa2<\tender_type_name>"
+ "<\token_id>83397<\token_id>" + "<\expired>true<\expired>"
+ "<\patron_tender><\patron_tenders>"; 

use the code:

    String response = "<\patron_tenders><\patron_tender name=\"alpha\" city=\"mumbai\" >"
+ "<\partial_card_number>************6319<\partial_card_number>"
+ "<\zip_code>123456<\zip_code>"
+ "<\tender_type_name>Visa<\tender_type_name>"
+ "<\token_id>80195<\token_id>" + "<\expired>false<\expired>"
+ "<\patron_tender>"+"<\patron_tender name=\"beta\" city=\"pune\" >"
+ "<\partial_card_number>************3545<\partial_card_number>"
+ "<\zip_code>565768<\zip_code>"
+ "<\tender_type_name>Visa2<\tender_type_name>"
+ "<\token_id>83397<\token_id>" + "<\expired>true<\expired>"
+ "<\patron_tender><\patron_tenders>"; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文