java xml annotation get name with namespace,< aaa:bbb> value< / aaa:bbb> [英] java xml annotation get field with namespace, <aaa:bbb>value</aaa:bbb>

查看:87
本文介绍了java xml annotation get name with namespace,< aaa:bbb> value< / aaa:bbb>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个没有架构的项目,我必须手动解析xml响应。
我的问题是我无法使用xml注释获得一些价值。

I'm working on a project that has no schema and I have to parsing the xml response manually. My problem is i can't get some value using the xml annotation.

例如,xml类似于:

<?xml version='1.0' encoding='UTF-8' ?>
<autnresponse>
    <action>QUERY</action>
    <response>SUCCESS</response>
    <responsedata>
        <autn:numhits>7</autn:numhits>
    </responsedata>
</autnresponse>

java类是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "autnresponse")
public class AutonomyResponse {
    private String action;
    private String response;
    private ResponseData responsedata;
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "responsedata")
public class ResponseData {
    @XmlElement(name = "numhits",namespace = "autn")
    private String numhits;
    @XmlElement(name = "totalhits")
    private String totalhits;
}

我可以获得动作和响应,但无法获得数字在responsedata中,
有人能告诉我如何使用注释处理< autn:numhits> 吗?
太多了谢谢!!!

I can get the action and the response, but can't get the numhits in the responsedata, Can anyone tell me how to handle the <autn:numhits> using annotation? Too much Thanks !!!

另一个问题是:我有多个< autn:numhits>在responsedata中 ....我怎样才能获得Java代码中的所有值。
- >我解决了这个多个相同的标签,只需设置列表和注释将自动生成列表

Another issue is : I have multi <autn:numhits> in the responsedata....how can i get all the value in the Java code. --> I solve this multi same tags, just set List and the annotation will automatically generate the list

推荐答案

事实是 autn - 只是前缀,不是命名空间为了正确处理XML文档,必须声明名称空间。

The fact is autn - is only prefix, not namespace. For correct processing of the XML document, namespace must be declared.

右名称空间声明:

<?xml version='1.0' encoding='UTF-8' ?>
<autnresponse xmlns:autn="http://namespace.here">
    <action>QUERY</action>
    <response>SUCCESS</response>
    <responsedata>
        <autn:numhits>7</autn:numhits>
    </responsedata>
</autnresponse>

您还需要更改注释:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "responsedata")
public class ResponseData {
    @XmlElement(name = "numhits",namespace = "http://namespace.here")
    private String numhits;
    @XmlElement(name = "totalhits")
    private String totalhits;
}

并为您提供最终建议。如果你有这个xml文档的xsd方案,请使用XJC utilit生成java代码。

And finnaly advice for you. If you have a xsd scheme for this xml document, use the XJC utilit for java code generation.

http://docs.oracle.com/javaee/5/tutorial/doc/bnbah.html

这篇关于java xml annotation get name with namespace,&lt; aaa:bbb&gt; value&lt; / aaa:bbb&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆