android xml 控件命名空间,android – 简单XML – 2个元素,同名不同的命名空间

我需要将XML字符串解析为对象.我会使用SimpleXML,但是我收到一个错误,在’url’私有java.lang.String com.example.rogedormans.xmlreader.XML.Alert.Channel.url字段上复制了名称’link’的注释.

具有相同问题的示例XML:

The Title

http://www.someurl.com

Some description

....

....

我google了很多,发现This,this和this stackoverflow文章,但没有一个对我有用…我也读了Simple XML documentation,但我不能让它工作.

如何将两个“链接”项目都放入我的对象中? (我认为这是命名空间的东西,但是什么?)

一个代码示例会很好!!!

解决方法:

您可以通过为Channel类实现Converter来解决此问题.

这是给你的一个例子.它缺少任何类型的错误检查等,并且仅使用单个Atom简化为Channel类.但是你会看到它是如何工作的.

带转换器的类通道

@Root()

@Convert(Channel.ChannelConverter.class) // Specify the Converter

public class Channel

{

@Element

private String title;

@Element

private String link;

@Element

private String description;

@Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom")

@Element()

private AtomLink atomLink;

// Ctor's / getter / setter ...

static class ChannelConverter implements Converter

{

@Override

public Channel read(InputNode node) throws Exception

{

Channel channel = new Channel();

InputNode child;

// Iterate over all childs an get their values

while( ( child = node.getNext() ) != null )

{

switch(child.getName())

{

case "title":

channel.setTitle(child.getValue());

break;

case "description":

channel.setDescription(child.getValue());

break;

case "link":

/*

* "link" can be either a ... or

* a

*/

if( child.getPrefix().equals("atom") == true )

{

AtomLink atom = new AtomLink();

atom.setHref(child.getAttribute("href").getValue());

atom.setRel(child.getAttribute("rel").getValue());

atom.setType(child.getAttribute("type").getValue());

channel.setAtomLink(atom);

}

else

{

channel.setLink(child.getValue());

}

break;

default:

throw new RuntimeException("Unknown Element found: " + child);

}

}

return channel;

}

@Override

public void write(OutputNode node, Channel value) throws Exception

{

/*

* TODO: Implement if necessary

*/

throw new UnsupportedOperationException("Not supported yet.");

}

}

@Root

public static class AtomLink

{

@Attribute

private String href;

@Attribute

private String rel;

@Attribute

private String type;

// Ctor's / getter / setter ...

}

}

所有内部类也可以作为通常的类实现.如果(去)序列化的事情很复杂,你也可以在转换器中使用串行器.

用法

最后一个演示:

final String xml = " \n"

+ "

The Title\n"

+ " http://www.someurl.com\n"

+ " Some description\n"

+ " \n"

+ " ....\n"

+ " ....\n"

+ " \n";

Serializer ser = new Persister(new AnnotationStrategy());

// ^----- Important! -----^

Channel c = ser.read(Channel.class, xml);

System.out.println(c);

请注意,转换器需要一个策略(有关详细信息,请参阅上面的链接);我使用了AnnotationStrategy,因为你可以简单地使用@Convert().必须在XML中的某个位置定义xmlns,否则您将捕获异常;我将它放入< atom:link ... />这里.

产量

Channel{title=The Title, link=http://www.someurl.com, description=Some description, atomLink=AtomLink{href=http://dunno.com/rss.xml, rel=self, type=application/rss+xml}}

标签:android,xml-parsing,xml-namespaces,simple-framework

来源: https://codeday.me/bug/20190702/1355881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值