Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JAXB解组列表_Java_List_Jaxb_Azure - Fatal编程技术网

Java 使用JAXB解组列表

Java 使用JAXB解组列表,java,list,jaxb,azure,Java,List,Jaxb,Azure,我知道这是一个初学者的问题,但我已经把头撞在墙上两个小时了,想弄明白这个问题 我从REST服务(Windows Azure管理API)返回的XML如下所示: <HostedServices xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <HostedService> <Url>htt

我知道这是一个初学者的问题,但我已经把头撞在墙上两个小时了,想弄明白这个问题

我从REST服务(Windows Azure管理API)返回的XML如下所示:

<HostedServices
  xmlns="http://schemas.microsoft.com/windowsazure"
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <HostedService>
    <Url>https://management.core.windows.net/XXXXX</Url>
    <ServiceName>foo</ServiceName>
  </HostedService>
  <HostedService>
    <Url>https://management.core.windows.net/XXXXX</Url>
    <ServiceName>bar</ServiceName>
  </HostedService>
</HostedServices>
下面是Java类:

@XmlRootElement(name="HostedServices", namespace="http://schemas.microsoft.com/windowsazure")
public class HostedServices
{
  private List<HostedService> m_hostedServices = new ArrayList<HostedService>();

  @XmlElement(name="HostedService")
  public List<HostedService> getHostedServices()
  {
    return m_hostedServices;
  }

  public void setHostedServices(List<HostedService> services)
  {
    m_hostedServices = services;
  }
}

@XmlType
public class HostedService
{
  private String m_url;
  private String m_name;

  @XmlElement(name="Url")
  public String getUrl()
  {
    return m_url;
  }

  public void setUrl(String url)
  {
    m_url = url;
  }

  @XmlElement(name="ServiceName")
  public String getServiceName()
  {
    return m_name;
  }

  public void setServiceName(String name)
  {
    m_name = name;
  }

}
@XmlRootElement(name=“HostedServices”,命名空间=”http://schemas.microsoft.com/windowsazure")
公共类寄宿服务
{
private List m_hostedServices=new ArrayList();
@XmlElement(name=“HostedService”)
公共列表getHostedServices()
{
返回m_托管服务;
}
公共服务(列表服务)
{
m_hostedServices=服务;
}
}
@XmlType
公共类寄宿服务
{
私有字符串m_url;
私有字符串m_name;
@xmlement(name=“Url”)
公共字符串getUrl()
{
返回m_url;
}
公共void setUrl(字符串url)
{
m_url=url;
}
@xmlement(name=“ServiceName”)
公共字符串getServiceName()
{
返回m_名称;
}
public void setServiceName(字符串名称)
{
m_name=名称;
}
}

任何帮助都将不胜感激。

@XmlRootElement
命名空间
不会传播给其子级。您应该显式指定命名空间:

...
@XmlElement(name="HostedService", namespace="http://schemas.microsoft.com/windowsazure") 
...
@XmlElement(name="Url", namespace="http://schemas.microsoft.com/windowsazure") 
...
@XmlElement(name="ServiceName", namespace="http://schemas.microsoft.com/windowsazure")
... 

谢谢这解决了问题。但这真的是标准做法吗?我在网上看到的各种JAXB示例似乎从未包含名称空间。还有一个
@XmlSchema
包级注释。它为包中的所有类指定默认名称空间。
...
@XmlElement(name="HostedService", namespace="http://schemas.microsoft.com/windowsazure") 
...
@XmlElement(name="Url", namespace="http://schemas.microsoft.com/windowsazure") 
...
@XmlElement(name="ServiceName", namespace="http://schemas.microsoft.com/windowsazure")
...