如何在 Eclipse 中使用 xstream 进行 xml 序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7878745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:13:35  来源:igfitidea点击:

How to use xstream fro xml serialization in eclipse

javaxmleclipsexstream

提问by Daniel

I am having some trouble figuring out how to download and use xstream in eclipse. When I download the binary distribution from the website, I get a zip file. Where should I extract it, and how do I use it?

我在弄清楚如何在 eclipse 中下载和使用 xstream 时遇到了一些麻烦。当我从网站下载二进制发行版时,我得到了一个 zip 文件。我应该在哪里提取它,以及如何使用它?

Edit:

编辑:

OK, I have added it to my build path, but now when I try to use XStream to serialize an ArrayList<Earmark>where Earmarkis a class that I have defined, I get the following error message:

好的,我已经将它添加到我的构建路径中,但是现在当我尝试使用 XStream 来序列化我定义的ArrayList<Earmark>whereEarmark是一个类时,我收到以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:334)
at examples.TestEarmarkExtractor.SerializeEarmarks(TestEarmarkExtractor.java:19)
at examples.TestEarmarkExtractor.main(TestEarmarkExtractor.java:48)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

Update: The error is occurring beforeI try the serialization. It occurs on the line

更新:我尝试序列化之前发生错误。它发生在线路上

XStream xs = new XStream();

回答by mre

  1. Extract the zip file to a directory (e.g. C:\Program Files\XStream)
  2. In eclipse, right-click on your project and select Build Path > Configure Build Path...
  3. Select Librariestab
  4. Select Add External Jars..
  5. Navigate to the libfolder of your XStream download and select the jar(s) to be added
  6. Select Open
  7. Select OK
  1. 将 zip 文件解压缩到一个目录(例如C:\Program Files\XStream
  2. 在 Eclipse 中,右键单击您的项目并选择 Build Path > Configure Build Path...
  3. 选择Libraries标签
  4. 选择 Add External Jars..
  5. 导航到libXStream 下载的文件夹并选择要添加的 jar
  6. 选择 Open
  7. 选择 OK

And here's the Two Minute Tutorialon how to use XStream.

这是关于如何使用 XStream的两分钟教程

回答by Alex Perevozchykov

Got this exception, then I download kxml2-min-2.3.0.jar and add it as a library and now it works (using xstream 1.4.4.jar)

得到这个异常,然后我下载 kxml2-min-2.3.0.jar 并将其添加为库,现在它可以工作(使用 xstream 1.4.4.jar)

回答by Aliti

You can add this library:

你可以添加这个库:

    <dependency>
        <groupId>xmlpull</groupId>
        <artifactId>xmlpull</artifactId>
        <version>1.1.3.1</version>
    </dependency>

回答by Peter

You need to unzip the .zip file to get at the .jar file, which you put on your classpath for your project in eclipse (Google if you don't know how to add third party library .jars to your project classpath in Eclipse).

您需要解压缩 .zip 文件以获取 .jar 文件,您将其放在 eclipse 中项目的类路径中(如果您不知道如何将第三方库 .jars 添加到 Eclipse 中的项目类路径,请使用谷歌) .

Once it is added to your project's classpath, you use the classes contained within it as you would any other classes, and the documentation on the XStream website tells you how to use them.

一旦将其添加到项目的类路径中,您就可以像使用任何其他类一样使用其中包含的类,并且 XStream 网站上的文档会告诉您如何使用它们。

Good choice by the way, XStream is great!

顺便说一句不错的选择,XStream很棒!

回答by user3519392

I had added the two libraries: dom4j-2.0.0-ALPHA-2.jarand xstream-1.4.7.jarand the same issue were displayed. After adding the kxml2-min-2.3.0.jarthe issue is not happening.

我添加了两个库:dom4j-2.0.0-ALPHA-2.jar并且xstream-1.4.7.jar显示了相同的问题。添加后kxml2-min-2.3.0.jar问题没有发生。