【问题标题】:Jaxb not unmarshalling xmlJaxb 不解组 xml
【发布时间】:2016-07-19 09:23:37
【问题描述】:

我尝试将一个 xml 解组到 ArrayList 或 Class 但我创建的对象没有被 JAXB 填充

@Override
public Ships load(String xmlFilePath) {
//Ships ship = new Ships();
ArrayList<Ship> shps = new ArrayList<>();
     try {  
     File file = new File(xmlFilePath);
     JAXBContext jaxbContext = JAXBContext.newInstance(Ship.class);

     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
     shps =  (ArrayList<Ship>) jaxbUnmarshaller.unmarshal(file);


  } catch (JAXBException e) {
    e.printStackTrace();
  }

return shps;}
}

【问题讨论】:

  • 您的 Ship 类和原始输入 xml 是什么?此外,您不应该将其转换为 ArrayList。您最终可能会获得 CCE。
  • xml 来自这个 Ship 但可能是 Ship 是接口的问题?当我尝试对 ArrayList 执行某些操作时,我在程序末尾只得到 NullPointerExeption。

标签: java xml jaxb


【解决方案1】:

也许您已经在其他地方读过这个解决方案,但最常见的解决方案是制作一个包装类(或者更好的是,一个通用的、可重用的包装类)。在下面的代码中有一个这个概念的工作示例,我希望代码是自我解释的。

package com.quick;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // just to avoid the get and set methods
class Wrapper<T> {
    @XmlAnyElement
    List<T> list;
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // just to avoid the get and set methods
class Ship {
    String id = "N" + System.nanoTime();
}

public class Main {

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

        // context, marshaller and unmarshaller
        JAXBContext context = JAXBContext.newInstance(Ship.class, Wrapper.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // original list
        List<Ship> originalList = new ArrayList<>();
        originalList.add(new Ship());
        originalList.add(new Ship());
        originalList.add(new Ship());

        // wrapper object
        Wrapper<Ship> originalWraper = new Wrapper<>();
        originalWraper.list = originalList;

        // marshal the original wrapper and save the bytes in byteArray1
        ByteArrayOutputStream os1 = new ByteArrayOutputStream();
        marshaller.marshal(originalWraper, os1);
        byte[] byteArray1 = os1.toByteArray();

        // unmarshall the bytes
        ByteArrayInputStream is = new ByteArrayInputStream(byteArray1);
        Wrapper<Ship> unmarshaledWrapper = (Wrapper<Ship>) unmarshaller.unmarshal(is);

        // THIS LIST SHOULD CONTAIN A COPY OF originalList
        List<Ship> unmarshaledList = unmarshaledWrapper.list;

        // marshal the unmarshaledWrapper (...) and sabe the bytes in byteArray2
        ByteArrayOutputStream os2 = new ByteArrayOutputStream();
        marshaller.marshal(unmarshaledWrapper, os2);
        byte[] byteArray2 = os2.toByteArray();

        // print the bytes, they should be the same
        System.out.println(new String(byteArray1));
        System.out.println(new String(byteArray2));
    }
}

一般的想法是创建一个类作为列表(或数组)的包装器,可以根据您的需要进行编组或编组。

【讨论】:

    猜你喜欢
    • 2013-11-15
    • 2011-10-18
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多