java jaxb意外元素解组

时间:2018-03-19 05:33:32

标签: java jaxb

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

我的工作场景是这样的:

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

现在我收到这样的错误:

  

意外元素(uri:" urn:Adapter-v3",local:"设置")。预期元素为< {}设置>,< {} 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;
    }

}

1 个答案:

答案 0 :(得分:1)

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

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

(在package-info.java。)