简体   繁体   中英

java jaxb unexpected element unmarshalling

Why is it necessary to use the ObjectFactory.java when using JAXB?

My work scenario is like this:

I am doing a conversion project from .NET to Java. In .NET the classes are already written similar to the POJOs. I just added the annotations (like @XmlRoot , @XmlElememnt etc) to the code. And I resolved the annotations related errors.

Now I am getting error like this:

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

XML file is as below:

<?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 class:

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

}

You are missing a namespace. Either you add namespace = "urn:Adapter-v3" to all the @XmlElement s and co. everywhere. Or simply declare the default namespace for the whole package:

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

(In the package-info.java .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM