Tag: simple framework

使用多个元素名称反序列化的简单xml框架

我试图使用Simple XML Framework将公共Web服务中的一些xml数据序列化为java对象。 问题是来自服务的不同方法返回具有不同元素名称的相同概念。 例如,方法A像这样返回元素foo foo value 而方法B返回 foo value 和方法C返回 foo value 是否有任何方法(多个名称注释等)将此xml反序列化为同一个类和相同的元素? 例如,将三个方法的结果反序列化为三个不同“Foo”对象中的相同“foo”元素(每个方法一个): @Root(name=”data”) public class Foo{ @Element public String foo; (…) }

SimpleFramwork XML:具有内部文本和子元素的元素

我在使用无法更改的特定格式的SimpleFramework反序列化xml时有以下情况… THIS INNER TEXT IS THE ISSUE 45.7 42.7 45.7 42.7 这是我为Question写的课程 @Root (name=”Question”) public class Question { @Attribute (name=”ID”) private String id; @ElementList (inline=true, required=false) private List criteria; @Text private String text; // And their getter and setters… } 现在的问题是, 我无法获得内部文字…… 任何人都可以建议我这样做…… ???

Simple-XML – 如何序列化集合中的派生类?

我想序列化一个对象层次结构,包括一个派生自基类“Thing”的对象列表。 这很好用,包括反序列化 – 但XML-Simple坚持编写一个指定实际使用的Java类的属性 当我使用下面的java代码创建一个xml文件时,内容如下: 212 但这不是我想要的。 我想要 212 没有class属性的’apple’和’orange’元素,不具有这种属性的’fruit’。 这可能吗? (第二个xml符合现有架构;添加额外属性不是一个选项) 这是代码: package com.mumpitz.simplexmltest; import java.io.File; import java.util.ArrayList; import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; class Fruit { @Attribute(name = “id”) protected final int id; Fruit( @Attribute(name = “id”) int id) { this.id = id; } int getObjectId() { […]

SimpleXML框架:带元素或文本的元素

我必须解析XML,它可以有两种forms: 要么: some text 我怎样才能做到这一点? 当我尝试在类中声明负责Command解析的@Element和@Text时,当我尝试将XML解析为此类的实例时抛出exception。 我当前的代码版本: @Root(name = “Command”, strict = false) public class AppCommand { @Element(name = “Variable”, required = false) @Getter private Variable variable; @Text(required = false) @Getter private String content; } 例外是: Text annotation @org.simpleframework.xml.Text(required=false, empty=, data=false) on field ‘content’ private java.lang.String com.example.AppCommand.content used with elements in class com.example.AppCommand

XPath(SimpleXML)根据孩子的兄弟姐妹的价值选择孩子

我正在使用SimpleXML(Java),我正在尝试根据列表中某个兄弟的值获取对象列表。 那么,这是我的XML: Service_Links http://www.stackoverflow.com Stack Overflow http://www.google.com Google Article_Links … … 我想要做的是使用XPath创建一个SimpleXML注释,以获得列表的兄弟“ittype”等于“Service_Links”的所有“链接”的列表。 因此,例如,这只有在我尝试静态获取第一个“资源” –节点(而不是根据它的兄弟“ittype”动态获取“资源” –节点时),这是有效的: @ElementList(name=”links”) @Path(“metadata/resources[1]/resource”) public List link; 我已经尝试了很多方法来格式化xpath来动态地找到“资源” –我想要的节点,我不可能在这里列出它们; 然而,这是我试过的一个例子,在我看来应该有用; 不知道我做错了什么…… @ElementList(name=”links”) @Path(“metadata/resources[resource/ittype=’Service_Links’]/resource”) public List link; 我已经谷歌搜索了一下,包括在这里的SO,并且发现不太相似; 而我所找到的,并没有很好的翻译,以便我发现我需要做什么。 例如,即使它非常相似, 这个SO似乎也不起作用。 更新#1(8OCT @ 12:38 pm) 根据反馈,我试过这个: @ElementList @Path(“metadata/resources/resource[ittype=’Service_Links’]”) public List link; 没有骰子:org.simpleframework.xml.core.PathException:字段’links’中路径’metadata / resources / resource [ittype =’Service_Links’]’的索引无效

如何从使用Simple的自定义Converter访问字段注释?

我正在使用Simple(http://simple.sourceforge.net/)库来编组/解组Java中的XML数据。 对于我的一些更复杂的数据结构,我需要编写自己的转换器。 例如,假设我有一个List<List>我需要编组。 我写了以下内容: class WorldObject { @Element(name=”vector-names”) @Convert(ListListConverter.class) private List<List> vectorNames; /** Constructor and other details … **/ } 与ListListConverter一起(我暂时遗漏了unmarshaller): class ListListConverter implements Converter<List<List>> { @Override public List<List> read(InputNode node) throws Exception { // stub return null; } @Override public void write(OutputNode node, List<List> value) throws Exception { node.setName(“list-list-string”); for (List list : value) […]

使用SimpleXml在序列化之前筛选列表

我正在尝试序列化包含child列表的parent 。 这些child节点有一个activated布尔字段,我希望生成的XML只包含activated属性设置为true的child节点 我可以通过复制parent对象并在过程中过滤child进程来做到这一点(我最后可能会这样做),但我想知道是否可以自定义SimpleXML以获得相同的结果。 编辑 我使用了ollo给出的回答。 我刚刚更改了Converter实现以使用子类的原始注释。 新的Converter实现: public class ChildConverter implements Converter { @Override public Child read(InputNode node) throws Exception { throw new UnsupportedOperationException(“Not supported yet.”); } @Override public void write(OutputNode node, Child value) throws Exception { if( value.isActived() == true ) // Check if ‘activated’ flag is set { // Set valus of […]

使用Simple XML(org.simpleframework.xml)序列化第三方类

我决定使用Simple XML序列化,并且遇到了基本问题。 我试图将java.util.UUID类实例序列化为这个小类中的f​​inal字段: @Root public class Identity { @Attribute private final UUID id; public Identity(@Attribute UUID id) { this.id = id; } } 教程显示如何通过注册这样的转换器来序列化第三方对象: Registry registry = new Registry(); registry.bind(UUID.class, UUIDConverter.class); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); serializer.write( object, stream ); 适用于UUID的转换器非常简单: public class UUIDConverter implements Converter { @Override public UUID […]

使用“已使用元素”解析XML Feed模具

我正在尝试使用Android中的SimpleXML解析XML Feed: http : //backend.deviantart.com/rss.xml? type = defiation&q = by%3Aspyed+sort%3Atime+meta% 3Aall 样品在这里: DeviantArt: spyed’s http://www.deviantart.com/?order=5&q=by%3Aspyed DeviantArt RSS for sort:time by:spyed en-us Copyright 2015, DeviantArt.com Thu, 20 Aug 2015 07:45:31 PDT DeviantArt.com http://blogs.law.harvard.edu/tech/rss http://sofzh.miximages.com/java/android-192.png Happy 15th Birthday! http://spyed.deviantart.com/journal/Happy-15th-Birthday-552416478 http://spyed.deviantart.com/journal/Happy-15th-Birthday-552416478 Sun, 09 Aug 2015 01:41:54 PDT Happy 15th Birthday! nonadult journals/personal spyed http://sofzh.miximages.com/java/spyed.gif Copyright 2015 spyed In […]

使用SimpleFramework解析日期

我收到一个XML响应,其属性包含以下值: Wed Sep 05 10:56:13 CEST 2012 我在我的模型类中定义了一个带注释的字段: @Attribute(name = “regDate”) private Date registerDate; 但是它引发了一个例外: java.text.ParseException: Unparseable date: “Wed Sep 05 10:56:13 CEST 2012” (at offset 0) 是否可以在SimpleFramework的注释中定义日期格式? 该日期字符串应包含哪种格式?