简单 XML - 如何从内部元素开始获取数据?
Simple XML - how to get data starting from an inner element?
我正在尝试使用 simple xml 为 android 从 xml 创建元素,我对根元素不感兴趣,只对一些嵌套元素感兴趣。我只对从下面 xml.
获取帐户对象感兴趣
<response xmlns="http://abc.abcdef.com/rest/xyz">
<request>
<channel>334892326</channel>
<number>486</number>
</request>
<status>
<code>200</code>
</status>
<results>
<account>
<creationTimestamp>2014-01-12T1:31:07Z</creationTimestamp>
<category>
<type>User-1</type>
<name>User-1</name>
</category>
</account>
<results>
</response>
我已经在 bean 中尝试了以下操作,但我得到的对象包含的所有值都是 null。
@Root(strict = false, name = "account")
@Path("response/results/account")
public class Account implements Serializable {
@Element(required = false)
private String creationTimestamp;
@Element(required = false)
private Category category;
}
最初指定 XML 格式不正确。但主要问题是由 Path 注释引起的 - “...将属性和元素映射到关联的字段或方法”,它不适用于 classes,仅适用于方法和字段。
所以这段代码很容易解析你的 XML 结构(简化版):
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;
import java.io.File;
@Root(strict = false)
public class Account {
@Element
@Path("results/account")
String creationTimestamp;
@Element
@Path("results/account")
Category category;
public static void main(String[] args)
throws Exception
{
Account account = new Persister().read(Account.class, new File("example.xml"));
System.out.println(account.creationTimestamp);
System.out.println(account.category.type);
System.out.println(account.category.name);
}
}
@Root
class Category {
@Element
String type;
@Element
String name;
}
不幸的是,@Path注解无法提取为class注解,这就是为什么你必须为每个字段都写它。
我正在尝试使用 simple xml 为 android 从 xml 创建元素,我对根元素不感兴趣,只对一些嵌套元素感兴趣。我只对从下面 xml.
获取帐户对象感兴趣<response xmlns="http://abc.abcdef.com/rest/xyz">
<request>
<channel>334892326</channel>
<number>486</number>
</request>
<status>
<code>200</code>
</status>
<results>
<account>
<creationTimestamp>2014-01-12T1:31:07Z</creationTimestamp>
<category>
<type>User-1</type>
<name>User-1</name>
</category>
</account>
<results>
</response>
我已经在 bean 中尝试了以下操作,但我得到的对象包含的所有值都是 null。
@Root(strict = false, name = "account")
@Path("response/results/account")
public class Account implements Serializable {
@Element(required = false)
private String creationTimestamp;
@Element(required = false)
private Category category;
}
最初指定 XML 格式不正确。但主要问题是由 Path 注释引起的 - “...将属性和元素映射到关联的字段或方法”,它不适用于 classes,仅适用于方法和字段。
所以这段代码很容易解析你的 XML 结构(简化版):
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;
import java.io.File;
@Root(strict = false)
public class Account {
@Element
@Path("results/account")
String creationTimestamp;
@Element
@Path("results/account")
Category category;
public static void main(String[] args)
throws Exception
{
Account account = new Persister().read(Account.class, new File("example.xml"));
System.out.println(account.creationTimestamp);
System.out.println(account.category.type);
System.out.println(account.category.name);
}
}
@Root
class Category {
@Element
String type;
@Element
String name;
}
不幸的是,@Path注解无法提取为class注解,这就是为什么你必须为每个字段都写它。