使用JAXB解组列表(Unmarshalling a list using JAXB)

编程入门 行业动态 更新时间:2024-10-24 17:23:45
使用JAXB解组列表(Unmarshalling a list using JAXB)

我知道这是一个初学者的问题,但我现在试图弄清楚这一点,我已经把头撞到了墙上两个小时。

我已经从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>

当我尝试使用JAXB解组它时,服务列表总是空的。

我想尽可能避免编写XSD(Microsoft不提供)。 这是JAXB代码:

JAXBContext context = JAXBContext.newInstance(HostedServices.class, HostedService.class); Unmarshaller unmarshaller = context.createUnmarshaller(); HostedServices hostedServices = (HostedServices)unmarshaller.unmarshal(new StringReader(responseXML)); // This is always 0: System.out.println(hostedServices.getHostedServices().size());

这里是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; } }

任何帮助将真诚感谢。

I know this is a beginner question, but I've been banging my head against a wall for two hours now trying to figure this out.

I've got XML coming back from a REST service (Windows Azure Management API) that looks like the following:

<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>

When I try to un-marshal it using JAXB, the list of services is always empty.

I would like to avoid writing an XSD if possible (Microsoft doesn't provide one). Here is the JAXB code:

JAXBContext context = JAXBContext.newInstance(HostedServices.class, HostedService.class); Unmarshaller unmarshaller = context.createUnmarshaller(); HostedServices hostedServices = (HostedServices)unmarshaller.unmarshal(new StringReader(responseXML)); // This is always 0: System.out.println(hostedServices.getHostedServices().size());

And here are the Java classes:

@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; } }

Any help would be sincerely appreciated.

最满意答案

@XmlRootElement的namespace不会传播给它的子@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") ...

@XmlRootElement's namespace is not propagated to its children. You should specify the namespace explicitly:

... @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") ...

更多推荐

本文发布于:2023-08-07 21:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   JAXB   list   Unmarshalling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!