标签: xstream

在我的应用程序中用JAXB替换XStream

目前在应用程序中,我们获得了一个SOAP响应xml,其中每个元素都附加了名称空间 我已经硬编码到我的代码中,因为应用程序需要干净的xml,所以会忽略命名空间.但是在测试时它被认为是一个弱解决方案,因为命名空间可能会在未来发生变化.我建议使用jaxb.我目前正在使用xtream,因为我们可以直接读取xml.所以我开始研究jaxb.但是jaxb需要xsd.我已经理解了这个过程,但我不确定如何在我的应用程序中实现Jaxb,因为我得到了一个xml响应.所以,我的问题是否有可能在这种情况下用jaxb替换xtream?
谢谢

xml soap namespaces xstream jaxb

2
推荐指数
1
解决办法
4070
查看次数

Jersey Restful应用程序上的JAXB或Xstream

我想知道哪种解决方案更适合Jersey Rest Web服务.在某些情况下,JAXB无法处理某些类型.使用XStream更好吗?

xstream jaxb jersey

2
推荐指数
1
解决办法
3627
查看次数

使用XStream生成标头标记

我正在使用XStream实用程序将POJO转换为XML.

但是当我生成一个xml并尝试读取它以进行进一步处理时,它会抛出一个错误

Exception caused by : com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我用Google搜索并发现它缺少xml的标头标记.

<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)

在将Java对象转换为XML文件时如何添加上面的标题?

java xstream jaxb

2
推荐指数
1
解决办法
2195
查看次数

XStream反序列化具有层次结构对象列表的xml

我下面有要在XStream中反序列化的xml。

<comments>
  <B>
    <id>1</id>
    <name>Name 1</name>
  </B>
  <C>
    <id>2</id>
    <name>name 2</name>
    <desc>Desc 2</desc>
  </C>
  <B>
    <id>3</id>
    <name>name 3</name>
  </B>
</comments>
Run Code Online (Sandbox Code Playgroud)

对象层次结构如下

@XStreamAlias("comments")
class Comments {
  @XStreamImplicit
  List<A> a = new ArrayList<A>();
}

@XStreamAlias("A")
class A {

}

@XStreamAlias("B")
class B extends A {
  long id ;
  String name;
}

@XStreamAlias("C")
class C extends A {
  long id;
  String desc;
  String name;
}
Run Code Online (Sandbox Code Playgroud)

我拥有的反序列化代码是

XStream xstream = new XStream();
xstream.autodetectAnnotations(Boolean.TRUE);
xstream.alias("comments", Comments.class);
String comments= "path to comments xml";
Comments comments = …
Run Code Online (Sandbox Code Playgroud)

xml polymorphism list xstream hierarchical

2
推荐指数
1
解决办法
2192
查看次数

与XML有关的XStream arrayList

我不知道目前问题出在哪里.第一次使用xml,我在将ArrayList放入xml文件并从中取出时遇到了一些问题.

我找到了这个,我试着这样做:如何使用XStream将对象列表转换为XML文档

但不幸的是我失败了.这是我到目前为止:拥有ArrayList的类:

public class ElbowList{

private ArrayList<Elbow> elbows = new ArrayList<>();

public ElbowList(){
    elbows = new ArrayList<Elbow>();
}

public void setElbows(ArrayList<Elbow> elbows){
    this.elbows.clear();
    this.elbows = elbows;
}

public ArrayList<Elbow> getElbows() {
    return elbows;
}

public void add(Elbow elbow){
    elbows.add(elbow);
}
}
Run Code Online (Sandbox Code Playgroud)

保存到XML:

MainFrame mainFrame = (MainFrame) SwingUtilities.getWindowAncestor(SetupPanel.this);
            ElbowList elbowList = (ElbowList) mainFrame.getObjects().get(2); //get ElbowList object
            XStream xstream = new XStream();
            xstream.alias("elbow", Elbow.class);
            xstream.alias("elbows", ElbowList.class);
            xstream.addImplicitCollection(ElbowList.class, "elbows", Elbow.class);

            String xml = xstream.toXML(elbowList.getElbows());
            System.out.println(xml);

            try {
                PrintWriter out = new …
Run Code Online (Sandbox Code Playgroud)

java xml arraylist xstream

2
推荐指数
1
解决办法
8372
查看次数

使用XStream跳过与List相关的标记

我正在尝试使用XStream将类实例转换为XML.一切都很好,除了一件事,我希望有人能帮助我解决.

作为一个前提,我有许多"嵌套"类(按顺序,OrderStatusImport- > OrderCollection- > Order- > OrderLine),但焦点由类表示Order; 如上所述,它的每个实例都包含一个OrderLine对象列表.就像这样:

public class Order {
   //attribute's declarations...
   ArrayList<OrderLine> orderLines;
}
Run Code Online (Sandbox Code Playgroud)

这是我的格式化程序类,我在其中调用主XStream逻辑:

private String createImportXml(OrderStatusImport orderStatusImport) {
    Object xstream = null;
    if (xstream == null) {
        xstream = new XStream() {
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new UpperCaseMapper(next);
            }
        };
    }

    ((XStream) xstream).alias("OrderStatusImport", OrderStatusImport.class);
    ((XStream) xstream).alias("OrderCollection", OrderCollection.class);
    ((XStream) xstream).alias("Order", Order.class);
    ((XStream) xstream).alias("OrderLine", OrderLine.class);
    ((XStream) xstream).omitField(Order.class, "OrderLines");

    String decl = "\n";
    String header …
Run Code Online (Sandbox Code Playgroud)

java xstream

2
推荐指数
1
解决办法
1305
查看次数

如何将Java库包含到Scala项目中

我是scala的新手。我想使用java xstream库将scala对象序列化为xml。(这是个好主意还是有更好的方法?)

如何安装该库,以便我可以导入:

import com.thoughtworks.xstream.io.{HierarchicalStreamReader, HierarchicalStreamWriter}
Run Code Online (Sandbox Code Playgroud)

现在thinkworks尚未定义。

是否在build.sbt中添加一个URL?还是有一个位置可以复制xstreams.jar?

scala xstream

2
推荐指数
1
解决办法
470
查看次数

如何使用XStream解组Map

我似乎无法使用XStream正确地解组Map.

我有以下代码:

存储类

public class Storage
{

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private Map<String, Repository> repositories = new LinkedHashMap<String, Repository>();

...

}
Run Code Online (Sandbox Code Playgroud)

存储库类

public class Repository
{

    @XStreamAsAttribute
    private String name;

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private int policy;

    @XStreamAsAttribute
    private int layout;

    @XStreamAsAttribute
    private int type;

...

}
Run Code Online (Sandbox Code Playgroud)

ConfigurationParser类

public class ConfigurationParser
{

    public Storage parseConfiguration(String xmlFile)
            throws FileNotFoundException
    {
        FileInputStream fis = new FileInputStream(xmlFile);

        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);

        xstream.alias("storage", Storage.class);
        xstream.alias("repositories", Map.class);
        xstream.alias("repository", Repository.class);

        return (Storage) …
Run Code Online (Sandbox Code Playgroud)

java xstream maven

1
推荐指数
1
解决办法
9845
查看次数

为什么使用xstream同一类出现java.lang.ClassCastException

java.lang.ClassCastException:cn.yunnet.wxhotel.utils.TestMicropay无法强制转换为cn.yunnet.wxhotel.utils.TestMicropay

TestMicropayEntity:

   package cn.yunnet.wxhotel.utils;

public class TestMicropay {
    private String return_code;
    private String return_msg;
    public String getReturn_code() {
        return return_code;
    }
    public void setReturn_code(String return_code) {
        this.return_code = return_code;
    }
    public String getReturn_msg() {
        return return_msg;
    }
    public void setReturn_msg(String return_msg) {
        this.return_msg = return_msg;
    }
}
Run Code Online (Sandbox Code Playgroud)

---- XStream的

package me.chanjar.weixin.common.util.xml;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DoubleConverter;
import com.thoughtworks.xstream.converters.basic.FloatConverter;
import com.thoughtworks.xstream.converters.basic.IntConverter;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.security.NoTypePermission;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;

import java.io.Writer;

public class XStreamInitializer {

  public …
Run Code Online (Sandbox Code Playgroud)

xstream

1
推荐指数
1
解决办法
754
查看次数

黄瓜没有将日期字符串从功能文件中的数据表序列化到我的pojo中的LocalDate字段

我试图找出如何在我的步骤定义中从黄瓜功能文件中解析日期字段。

class Person{ 
  String name
  LocalDate dob
}

scenario: do something with people
Given  list of people:
                     |name|dob|
                     | john| 20-09-2001|

@Given("^list of people:")
public void doSomething(List<Person> people) {

}
Run Code Online (Sandbox Code Playgroud)

请注意,我无权访问Person类,我确定自己必须编写自己的转换器或注册某个库中某人编写的转换器,在搜索到的唯一选项后,我看到的是使用@Transform更改它们pojo在java.time.LocalDate字段上。

我目前收到以下异常

cucumber.runtime.CucumberException:              cucumber.deps.com.thoughtworks.xstream.converters.ConversionException: Cannot deserialize object with new readObject()/writeObject() methods
---- Debugging information ----
class               : java.time.LocalDate
required-type       : java.time.LocalDate
converter-type      : cucumber.deps.com.thoughtworks.xstream.converters.reflection.SerializableConverter
path                : /list/com.pkg.Person/dob
Run Code Online (Sandbox Code Playgroud)

我尝试将dateformat更改为yyyy-MM-dd,通常可以,但在这种情况下不行。我将不胜感激关于如何设置和注册自定义转换器的任何指示

我的黄瓜依赖项如下,如果有任何区别,我可以将其替换为较新版本。

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>2.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>2.4.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

java xstream cucumber cucumber-jvm cucumber-java

1
推荐指数
1
解决办法
1449
查看次数