gpt4 book ai didi

java jaxb意外元素解码

转载 作者:行者123 更新时间:2023-12-02 11:30:03 25 4
gpt4 key购买 nike

为什么使用JAXB时需要使用ObjectFactory.java

我的工作场景是这样的:

我正在做一个从 .NET 到 Java 的转换项目。在 .NET 中,类的编写方式与 POJO 类似。我刚刚将注释(例如 @XmlRoot@XmlElememnt 等)添加到代码中。我解决了与注释相关的错误。

现在我收到这样的错误:

unexpected element (uri:"urn:Adapter-v3", local:"Settings"). Expected elements are <{}Settings>, <{}TypeMapping>

XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<Settings version="3" xmlns="urn:Adapter-v3">
<Connections>
<Connection name ="A" description="DEV">
<SaveVersion version="M" siteURL="https://example.com" />
<Save>
<Id>id123</Id>
<Client>hello</Client>
</Save>
</Connection>
<Connection name ="B" description="DEV1">
<SaveVersion version="M" siteURL="https://example.com" />
<Auth>
<UserId>id123</UserId>
<Password>pass</Password>
</Auth>
</Connection>
</Connections>
<Mappings>
<Mapping cont="AA" auction="A1">
<Description>Desc</Description>
<Content
attr1="IO"
attr2="d"
attr3="Information"
attr4="false"

<Element enabled="false" count="200" prefix="DocLib_" itemPrefix="0" />
<Sub enabled="false" count="100" prefix="Folder_" itemPrefix="0" />
<FilenameA auction="N" delay="3" />
</Content>
</Mapping>
<Mapping cont="AB" auction="SharePointOLDev1">
<Description>Desc</Description>
<Content
attr1="IO"
attr2="d"
attr3="Information"
attr4="false"

<Element enabled="false" count="200" prefix="DocLib_" itemPrefix="0" />
<Sub enabled="false" count="100" prefix="1" itemPrefix="0" />
</Content>
</Mapping>
</Mappings>
<TypeMappings>
<TypeMapping Type="rfid" ext="msg" />
</TypeMappings>

</Settings>

POJO 类:

public class Settings {
@XmlElement(name="Connections", nillable=false, type=Connection.class)
public ArrayList<Connection> Connections;

@XmlElement(name="Mappings", nillable=false, type=Mapping.class)
public Mapping[] Mappings;

protected ArrayList<TypeMapping> TypeMappings;

public Mapping[] getMappings() {
return Mappings;
}

public void setMappings(ContentServerMapping[] contentServerMappings) {
Mappings = Mappings;
}

@XmlElement(name="TypeMappings")
public ArrayList<MIMETypeMapping> getTypeMappings() {
return TypeMappings;
}

public void setTypeMappings(ArrayList<TypeMapping> TypeMappings) {
TypeMappings = TypeMappings;
}

}

最佳答案

您缺少命名空间。您可以将 namespace = "urn:Adapter-v3" 添加到所有 @XmlElement 和 co 中。到处。或者简单地声明整个包的默认命名空间:

@javax.xml.bind.annotation.XmlSchema(
namespace = "urn:Adapter-v3",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.acme.foo;

(在package-info.java中。)

关于java jaxb意外元素解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49356382/

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