我需要生成这样的XML:
<Root>
<Children>
<InnerChildren>SomethingM</InnerChildren>
</Children>
</Root>
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案是在Root类上创建一个内部类:
@Root
class Root{
@Element
Children element;
@Root
private static class Children{
@Element
String innerChildren;
}
}
Run Code Online (Sandbox Code Playgroud)
但我想避免内部类创建,因为它会在使用Root对象时使事情看起来很奇怪.无论如何,我可以在不使用内部类的情况下实现该结果吗?
Root root = new Root("Something");
Run Code Online (Sandbox Code Playgroud)
我想避免的:
Children child = new Children("Something");
Root root = new Root(child);
// this could be achieve by injecting some annotations
// in the constructor, but it's awful
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用简单的xml(http://simple.sourceforge.net/)序列化一个对象.对象设置非常简单:
@Root(name = "order_history")
public class OrderHistory {
@Element(name = "id", required = false)
public int ID;
@Element(name = "id_order_state")
public int StateID;
@Element(name = "id_order")
public int OrderID;
}
Run Code Online (Sandbox Code Playgroud)
问题是当我创建这个没有ID的类的新实例时:
OrderHistory newhistory = new OrderHistory();
newhistory.OrderID = _orderid;
newhistory.StateID = _stateid;
Run Code Online (Sandbox Code Playgroud)
我通过简单的xml序列化它:
StringWriter xml = new StringWriter();
Serializer serializer = new Persister();
serializer.write(newhistory, xml);
Run Code Online (Sandbox Code Playgroud)
它仍然在生成的xml中读取0:
<?xml version='1.0' encoding='UTF-8'?>
<order_history>
<id>0</id>
<id_order>2</id_order>
<id_order_state>8</id_order_state>
</order_history>
Run Code Online (Sandbox Code Playgroud)
我猜这是因为ID属性不为null,因为整数不能为null.但我真的需要摆脱这个节点,我宁愿不要手动删除它.
有人提出任何线索吗?
我在android上的simplexml框架有一个奇怪的问题.我试图从xml源读取并填充一个名为weatherdata的对象.
XML文件(无法控制):
<weatherdata>
<product class="pointData">
....
</product>
</weatherdata>
Run Code Online (Sandbox Code Playgroud)
所以我的java文件看起来像:
@Root(name = "weatherdata", strict=false)
public class Weatherdata {
@Element(name="product", required = true)
protected ProductType product;
Run Code Online (Sandbox Code Playgroud)
但我得到一个非常奇怪的错误:
01-14 14:22:28.919: W/System.err(18011): java.lang.ClassNotFoundException: pointData in loader dalvik.system.PathClassLoader
Run Code Online (Sandbox Code Playgroud)
如果我在@Element字段中尝试class = ProductType.class,它并不关心.即使我使用构造函数在projet中创建pointData类,错误也不会改变.它看起来像"class"一样,是一个简单的xml框架2.6.2的保留关键字.
我无法改变xml.知道如何解决这个问题吗?谢谢你.
我在我的应用程序中使用了一个简单的XML序列化程序,似乎已经通过在proguard.cfg中使用以下行成功地将其从Proguard混淆中排除,这要归功于这个SO问题:
-keep public class org.simpleframework.**{ *; }
-keep class org.simpleframework.xml.**{ *; }
-keep class org.simpleframework.xml.core.**{ *; }
-keep class org.simpleframework.xml.util.**{ *; }
Run Code Online (Sandbox Code Playgroud)
我可以签名并导出apk,但是当我尝试运行我的应用程序时,在尝试访问res/raw目录(R.raw.home_screen_menu)中的XML文件时,它会因NullPointerException而崩溃.
我将R.java排除在外:
-keepclassmembers class **.R$* {
public static <fields>;
}
Run Code Online (Sandbox Code Playgroud)
我整天都在玩这个配置文件,但没有任何运气.有没有人遇到过这个问题或类似的问题?我尝试过的最新事情是使用如下所示的"adaptresource"选项:
-adaptresourcefilenames **.xml
-adaptresourcefilecontents **.xml
Run Code Online (Sandbox Code Playgroud)
作为参考,这是我的proguard.cfg的内容:
#Use 5 step of optimization
-optimizationpasses 5
#When not preverifing in a case-insensitive filing system, such as Windows. This tool will unpack your processed jars,(if using windows you should then use):
-dontusemixedcaseclassnames
#Specifies not to ignore non-public library …Run Code Online (Sandbox Code Playgroud) 我有这种格式的XML:
<element1>
some inner text
<child1>11</child1>
<child2>True</child2>
</element1>
Run Code Online (Sandbox Code Playgroud)
我尝试通过简单的框架将此XML映射到对象上:
@Element(name = "element1")
public static class Something {
@Element(required=false)
public int child1;
@Element(required=false)
public boolean child2;
}
Run Code Online (Sandbox Code Playgroud)
我无法更改XML,我需要在类中的某些字段上映射值"some inner text".不幸的是,我不知道该怎么做(如果可能的话).我试过@Text注释,但它不能和@Element一起用在一个类中.
谢谢你的帮助.
嗨,我有解析这个xml的问题:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Items>
<Item>
<TdlID>202</TdlID>
<TdlDesc>1234567890</TdlDesc>
<Due>2013-07-19</Due>
<Done>0</Done>
<Creadate>2013-07-08 23:43:51</Creadate>
<UsrLogin>demouser</UsrLogin>
<Prj>3211</Prj>
<Chngdate>2013-07-17</Chngdate>
</Item>
<Item>
<TdlID>218</TdlID>
<TdlDesc>fghjkljh</TdlDesc>
<Due>2013-07-31</Due>
<Done>1</Done>
<Creadate>2013-07-10 11:06:10</Creadate>
<UsrLogin>demouser</UsrLogin>
<Prj>abcd</Prj>
<Chngdate>2013-07-17</Chngdate>
</Item>
</Items>
<Status>
<code>0</code>
<message>Its Working!!!</message>
</Status>
</Response>
Run Code Online (Sandbox Code Playgroud)
我知道,这个问题在ElementList中是100%,但不知道,在哪里:(
package sk.jbase.todolist.xml;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Order;
import java.util.ArrayList;
import java.util.List;
@Element(name = "Items")
@Order(elements = "Item")
public class Items {
@ElementList(name = "Item")
protected List<Item> item;
public List<Item> getItems() {
if (item == null) {
item = new ArrayList<Item>();
}
return this.item; …Run Code Online (Sandbox Code Playgroud) 试图解析一个 XML 文件让我感到厌烦。它不是我的,所以我无法编辑它。先看一下(我把不需要的部分剪掉了)
01 <feed>
02 <id>urn:ya.ru:feed/friends/12345678</id>
03 <author>
04 <name>Master</name>
05 <uri>http://test.ya.ru</uri>
06 <y:id>urn:ya.ru:person/12345678</y:id>
07 </author>
08 <title>List of friends posts</title>
09 <updated>2013-08-02T19:14:00Z</updated>
10 <entry>
11 <id>urn:ya.ru:post/110554367/3744</id>
12 <author>
13 <name>MrBigJoker</name>
14 <y:id>urn:ya.ru:person/110554367</y:id>
15 </author>
16 </entry>
17 </feed>
Run Code Online (Sandbox Code Playgroud)
您可以在第 02、06、11 和 14 行看到ID标签。问题是我在第 14 行收到错误“元素ID已被使用”。
我使用 SimpleXML 和以下三个类:
@Root (name = "feed")
public class Feed { // All lines
@Element
private String id;
@Element
private Author_feed author;
@Element
private String title;
@Element
private String …Run Code Online (Sandbox Code Playgroud) 我使用SimpleXML for Java来解析对java类映射的XML响应.但是,我无法让这个特定的部分与我的Android设备一起工作.
我的XML片段看起来像这样,
<t:EmailAddresses>
<t:Entry Key="EmailAddress1">sip:xxx@abs.com</t:Entry>
<t:Entry Key="EmailAddress2">smtp:xxx@abs.com</t:Entry>
<t:Entry Key="EmailAddress3">SMTP:xxx@abs.com</t:Entry>
</t:EmailAddresses>
Run Code Online (Sandbox Code Playgroud)
我对EmailAddresses的类定义如下所示,
@Root
public class EmailAddresses
{
@ElementList
private List<Entry> Entry;
public List<Entry> getEntry() { return Entry; }
public void setEntry(List<Entry> entry) { Entry = entry; }
}
Run Code Online (Sandbox Code Playgroud)
我的Entry类看起来像这样,
@Element
public class Entry
{
@Attribute
private String Key;
public String getKey() { return Key; }
public void setKey(String key) { Key = key; }
}
Run Code Online (Sandbox Code Playgroud)
当我解析运行解析器时,我只获得了密钥,而且当我尝试将所有3解析为Entry类的List时,我得到"Multiple Root Elements"错误.
有人可以指出我正确的方向吗?谢谢 !!
注意:XML命名空间"t"已正确定义.
我正在尝试使用Retrofit下载XML数据并使用Simple解析它,然后将其加载到ListView中.
不幸的是,下载的数据不会出现在屏幕上 有人能告诉我问题出在哪里吗?
这是我的模特:
@Root(name = "item")
public class Article {
@Element(name = "title")
private String title;
@Element(name = "author")
private String author;
@Element(name = "description")
private String description;
Run Code Online (Sandbox Code Playgroud)
接口代码:
public interface Service {
@GET("/rss/news")
public void getArticle(Callback<List<Article>> callback);
}
Run Code Online (Sandbox Code Playgroud)
片段代码:
public class ArticlePreviewFragment extends Fragment {
protected ArticlePreviewAdapter adapter;
protected List<Article> previewList;
private ListView listView;
public ArticlePreviewFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
previewList = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) …Run Code Online (Sandbox Code Playgroud) 我正在使用Retrofit和SimpleXML来解析来自某些公共API的XML响应.我一直在很好地处理所有内容,直到我偶然发现包含自由文本和子标记的XML标记 - 如以下示例所示:
<a>
Some free-style text
<b>Even more text!</b>
</a>
Run Code Online (Sandbox Code Playgroud)
为了尝试使用Simple-XML注释进行反序列化,我已经采用了两种方式.请记住,基本上'a'是一个条目标签列表:
@ElementList(entry = "a", inline = true, required = false) List<A> aList;
Run Code Online (Sandbox Code Playgroud)
'A'定义如下:
public static class A {
@Text(required = false) protected String a;
}
Run Code Online (Sandbox Code Playgroud)
这很好地读取了自由文本部分,但是任何反序列化'b'标记内容的尝试(例如通过将@Elementw或o @Path注释成员添加到类'A')都失败了.我查看了SimpleXML文档,显然使用时存在以下限制@Text:
管理Text注释使用的规则是每个模式类只能有一个.此外,此注释不能与Element注释一起使用.只有属性注释可以与它一起使用,因为此注释不会在拥有元素中添加任何内容.
@ElementList(entry = "a", inline = true, required = false) List<String> aList;
Run Code Online (Sandbox Code Playgroud)
再一次,"a"标签的内容得到了正确的反序列化,但是没有办法找到"b"子标签的内容.
如何使用与JAVA对象相关的纯Simple-XML注释,使用关联的'b'子标签对'a'标签的内容进行反序列化?