gpt4 book ai didi

java - JAXB 解码中 XML 中的命名空间存在问题

转载 作者:行者123 更新时间:2023-11-30 03:32:50 27 4
gpt4 key购买 nike

我有一个 XML 需要使用 JAXB 进行解码。如果我从元素中删除所有命名空间属性,则代码可以正常工作,但如果保留命名空间属性,则在解码后会得到一个空对象。

XML 是这样的:

<Animal  xmlns="http://allmycats.com/serviceplatform/1.0/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Cat z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty</name>
</Cat>
<Cat z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty2</name>
</Cat>
</Animal>

我的动物 bean 是这样的:

    @XmlRootElement(name = "Animal")
public class Animal{
List<Cat> cats;

@XmlElement(name = "Cat")
public List<Cat> getCats() {
return cats;
}

public void setCats(List<Cat>cats) {
this.cats= cats;
}
}

猫 bean 就像:

@XmlRootElement(name = "Cat")
public class Cat {
private String zId;

@XmlAttribute(name = "z:Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
public String getzId() {
return zId;
}
public void setzId(String zId) {
this.zId = zId;
}

private String name;
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

在运行时,我得到一个空对象。我尝试从属性中删除 "z:" 并收到此异常:

org.xml.sax.SAXParseException; The prefix "z" for attribute "z:Id" associated with an element type "Cat" is not bound.]

如果我从 cat 和 Animal 中删除命名空间,则会出现此异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://allmycats.com/serviceplatform/1.0/", local:"Animal"). Expected elements are <{}Animal>

解码的最终代码如下。最后一行给出了空指针异常

File file = new File(filepath1);
System.out.println("file exists? : "+ file.exists()); // prints true

JAXBContext jaxbContext = JAXBContext.newInstance(Animal2.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Animal2 animals = (Animal2)jaxbUnmarshaller.unmarshal( file );
System.out.println("--file size: "+animals.getCats().size());

我不确定如何处理 POJO 类中的命名空间和 z: 属性。请问有什么建议吗?

如果我在 XML 中没有任何命名空间或任何带有命名空间的属性,则代码可以正常工作,但我无法更改 XML。

最佳答案

XMLAtribute 有属性名称,所以

@XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization").

根据您的 xml 判断,猫与动物位于同一命名空间中

以下代码适用于 JDK 7(修复了动物的 ns 和 zid 的属性名称)。

@XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
public class Animal2{
List<Cat2> cats;

@XmlElement(name = "Cat")
public List<Cat2> getCats() {
return cats;
}

public void setCats(List<Cat2>cats) {
this.cats= cats;
}
}
@XmlRootElement(name = "Cat")
public class Cat2 {
private String zId;

@XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
public String getzId() {
return zId;
}
public void setzId(String zId) {
this.zId = zId;
}

private String name;

@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

关于java - JAXB 解码中 XML 中的命名空间存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com