目前在应用程序中,我们获得了一个SOAP响应xml,其中每个元素都附加了名称空间 我已经硬编码到我的代码中,因为应用程序需要干净的xml,所以会忽略命名空间.但是在测试时它被认为是一个弱解决方案,因为命名空间可能会在未来发生变化.我建议使用jaxb.我目前正在使用xtream,因为我们可以直接读取xml.所以我开始研究jaxb.但是jaxb需要xsd.我已经理解了这个过程,但我不确定如何在我的应用程序中实现Jaxb,因为我得到了一个xml响应.所以,我的问题是否有可能在这种情况下用jaxb替换xtream?
谢谢
我想知道哪种解决方案更适合Jersey Rest Web服务.在某些情况下,JAXB无法处理某些类型.使用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文件时如何添加上面的标题?
我下面有要在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,我在将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) 我正在尝试使用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) 我是scala的新手。我想使用java xstream库将scala对象序列化为xml。(这是个好主意还是有更好的方法?)
如何安装该库,以便我可以导入:
import com.thoughtworks.xstream.io.{HierarchicalStreamReader, HierarchicalStreamWriter}
Run Code Online (Sandbox Code Playgroud)
现在thinkworks尚未定义。
是否在build.sbt中添加一个URL?还是有一个位置可以复制xstreams.jar?
我似乎无法使用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.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) 我试图找出如何在我的步骤定义中从黄瓜功能文件中解析日期字段。
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) xstream ×10
java ×5
jaxb ×3
xml ×3
arraylist ×1
cucumber ×1
cucumber-jvm ×1
hierarchical ×1
jersey ×1
list ×1
maven ×1
namespaces ×1
polymorphism ×1
scala ×1
soap ×1