我知道这是一个初学者的问题,但我现在试图弄清楚这一点,我已经把头撞到了墙上两个小时。
我已经从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") ...更多推荐
发布评论