7

亲爱的 Java 程序员,我使用了来自http://www.vogella.com/articles/JAXB/article.html的示例

用于我的 3 个类 UserStorage、User 和 UserTest 的 JAXB XML 使用

它工作正常,但它只是 unmarchialing

JAXBContext context = JAXBContext.newInstance(UserStorage.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

the User class is ABSTRACT!, so it throws an 

线程“主”javax.xml.bind.UnmarshalException 中的异常:无法创建 platform.User 的实例 - 带有链接异常:com.sun.xml.internal.bind.v2.runtime 的 [java.lang.InstantiationException]。 unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:648) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236) 在 com.sun.xml.internal.bind。 v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:615) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:170) 在 com.sun.xml。 internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:487) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext。startElement(UnmarshallingContext.java:465) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135) 在 com.sun.org.apache.xerces.internal.parsers。 AbstractSAXParser.startElement(AbstractSAXParser.java:501) 位于 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400) 位于 com.sun.org.apache.xerces.internal.impl。 XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) 在 com.sun.org.apache.xerces.internal。 impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl。scanDocument(XMLDocumentFragmentScannerImpl.java:511) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration。在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser 的 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) 解析 (XML11Configuration.java:737)。在 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522) 在 com.sun.xml.internal.bind.v2.runtime 解析(AbstractSAXParser.java:1205)。 unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173) 在 javax.xml.bind.helpers.AbstractUnmarshallerImpl。unmarshal(AbstractUnmarshallerImpl.java:137) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) at platform.UserTest.main(UserTest.java:77) 原因:java.lang.InstantiationException at sun .reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java :112) 在 com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext .java:609) ... 20 更多Platform.UserTest.main(UserTest.java:77) 处的 AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) 原因:java.lang 处 sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) 处的 java.lang.InstantiationException .reflect.Constructor.newInstance(Constructor.java:513) 在 com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:112) 在 com.sun.xml.internal.bind.v2.runtime .ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609) ... 20 更多Platform.UserTest.main(UserTest.java:77) 处的 AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194) 原因:java.lang 处 sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) 处的 java.lang.InstantiationException .reflect.Constructor.newInstance(Constructor.java:513) 在 com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:112) 在 com.sun.xml.internal.bind.v2.runtime .ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609) ... 20 更多com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:112) 的 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 的 InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)在 com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java: 609) ... 20 更多com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:112) 的 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 的 InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)在 com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java: 609) ... 20 更多createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609) ... 20 更多createInstance(ClassBeanInfoImpl.java:231) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609) ... 20 更多

有什么解决方案吗,所以我可以将 User 类绑定到 XML,因为我可以使用用户详细信息保存 XML 文件,但是当我想检索它时,它说明 User 类是抽象的,我有一个管理员,经纪人,股东子类,但到目前为止,在我的用户测试类中,我只创建了 4 个管理员进行测试,谢谢并希望您能提供帮助。

import platform.UserStorage;
import platform.User;



public class UserTest {

private static final String USER_XML = "user2.xml";

public static void main(String[] args) throws JAXBException, IOException {

    ArrayList<User> userList = new ArrayList<User>();

    // create test users
            User user1 = new Admin();
            user1.setName("Dave");
            user1.setPass("1234");
            user1.setDeleted(true);
            user1.setBan(false);
            userList.add(user1);

            User user2 = new Admin();
            user2.setName("James");
            user2.setPass("1234");
            user2.setDeleted(true);
            user2.setBan(false);
            userList.add(user2);

            User user3 = new Admin();
            user3.setName("Mike");
            user3.setPass("1234");
            user3.setDeleted(true);
            user3.setBan(false);
            userList.add(user3);


            // create bookstore, assigning book
            UserStorage userstore = new UserStorage();
            userstore.setListName("Test List");
            userstore.setUserList(userList);

            // create JAXB context and instantiate marshaller
            JAXBContext context = JAXBContext.newInstance(UserStorage.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(userstore, System.out);

            Writer w = null;
            try {
                w = new FileWriter(USER_XML);
                m.marshal(userstore, w);
            } finally {
                try {
                    w.close();
                } catch (Exception e) {
                }
            }

            // get variables from our xml file, created before
            System.out.println();
            System.out.println("Output from our XML File: ");
            Unmarshaller um = context.createUnmarshaller();
            UserStorage userstore2 = (UserStorage) um.unmarshal(new FileReader(
                    USER_XML));

            for (int i = 0; i < userstore2.getUsersList().toArray().length; i++) {
                System.out.println("User " + (i + 1) + ": "
                        + userstore2.getUsersList().get(i).getName() + " Pass "
                        + userstore2.getUsersList().get(i).getPass());
            }}  }



package platform;


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


//If you want you can define the order in which the fields are written
//Optional

/**
 * @author dinesh.kaushish, james.wyche //updated XML properties.
 * 
 */
@XmlRootElement(name = "user")
@XmlType(propOrder = { "name", "pass", "deleted", "ban" })

..

public abstract class User implements UserInterface {

private String name;
private String pass;
private boolean deleted;
private boolean ban;

/**
 * @ author dinesh.kaushish
 * @param String username
 * return void
 */

public void setName(String name)
{
    this.name = name;
}

// If you like the variable name, e.g. "name", you can easily change this
    // name for your XML-Output:

/**
 * @author dinesh.kaushish
 * @param null
 * @return String user;
 */
@XmlElement(name = "user")
public String getName()
{
    return this.name;
}

/**
 * @author dinesh.kaushish
 * @param String pwd
 * @return void
 */
public void setPass(String pass)
{
    this.pass=pass;
}

/**
 * @author dinesh.kaushish
 * @param void
 * @return String password
 */
@XmlElement(name = "pass")
public String getPass()
{
    return pass;
}

/**
 * @author dinesh.kaushish
 * @param dFlag
 * @return void
 */
public void setDeleted(boolean deleted)
{
    this.deleted = deleted;
}

/**
 * @author dinesh.kaushish
 * @return boolean isDeleted
 */
@XmlElement(name = "deleted")
public boolean getDeleted()
{
    return deleted;
}


/**
 * @author dinesh.kaushish
 * @param bFlag
 */
public void setBan(boolean ban)
{
    this.ban = ban;
}

/**
 * @author dinesh.kaushish
 * @return Boolean isBanned
 */
@XmlElement(name = "ban")
public Boolean getBan()
{
    return ban;
}


public abstract void addUser();
public abstract void removeUser();
public abstract void verifyUser();
public abstract void passReset();
public abstract void faultReport();
public abstract void RequestsForAccess();
public abstract void UpdateDetails();
public abstract void BanUser();
public abstract void ChangePermissions();


    }



package platform;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;





/**
 * @author michael.wambeek, james.wyche //added JAXB support.
 *
 */
//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(namespace = "platform")
public class UserStorage {

// XmLElementWrapper generates a wrapper element around XML representation
    @XmlElementWrapper(name = "userList")
    // XmlElement sets the name of the entities
    @XmlElement(name = "user")
private ArrayList<User> userList;
private String listName = "";

// 私有静态 UserStorage 实例;// james.wyche JAXB 代码结束。

public UserStorage(){

}

/**
 * @author michael.wambeek
 * 
 * Searches for a username and returns the password.
 * 
 * @param username The username to search for
 * @return The password of the correct username or null
 * @throws Exception
 */
public String findUser(String username) throws Exception{
    return search(username);
}

public boolean storeUser(String username, String password, UserType type){
    return true;
}

/**
 * @author james.wyche
 * @param userList
 */
public void setUserList(ArrayList<User> userList) {
    this.userList = userList;
}

/**
 * 
 * @return UserList
 */
public ArrayList<User> getUsersList() {
    return userList;
}
4

4 回答 4

10

为什么这会失败是因为 Jaxb 将尝试创建 User 的实例。这是抽象的,因此是失败的。

在您的抽象类上添加注释

@XmlTransient //Prevents the mapping of a JavaBean property/type to XML representation
@XmlSeeAlso({Admin.class, <other class>}) //Instructs JAXB to also bind other classes when binding this class

查看每个的 javadoc(XmlTransientXmlSeeAlso

这将阻止 jaxb 尝试初始化您的抽象类。

我发现这种方法的唯一缺点是会在创建的 xml 中添加额外的命名空间信息。

于 2012-04-22T12:28:59.327 回答
8

您需要将XmlSeeAlso注释添加到具有属性 Admin 的 User 类以及作为 User 类的子类的所有其他具体类。

@XmlSeeAlso({Admin.class})

PS,不要忘记将Xml标签@XmlRootElement添加到Admin类。

于 2012-04-22T12:29:31.840 回答
0

这是行不通的,因为 JAXB 在解组 xml 时必须创建类的新实例(对象)。如果 xml 中的标签绑定到抽象类,它就不能从该类实例化对象。您要么必须使 User 类非抽象,要么将 xml 标签绑定到 User 的具体子类。

于 2012-04-22T12:19:27.690 回答
0

您必须指定concrete每个元素的类型:

<user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Admin">
    ...
</user>
于 2017-02-06T14:38:18.027 回答