2016-07-19 5 views
1

私はたぶん、あなたはすでにどこかにこのソリューションを読んでのArrayListまたはクラスにするために1つのXMLをアンマーシャリングしようとするが、私が作るオブジェクトはJAXBJAXB非整列化ではないXML

@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;} 
} 
+0

Shipクラスと元の入力xmlは何ですか?また、ArrayListにキャストしてはいけません。最終的に最終的にCCEを取得することがあります。 – glee8e

+0

xmlはこの船から来たものですが、船がインターフェイスであるという問題になりますか?そして、私はArrayListで何かしようとすると、プログラムの終わりにNullPointerExeptionしか得られません。 –

答えて

0

によって移入されていませんが、最も一般的な解決策ラッパークラスを作ることです(または、より良い、一般的な、再利用可能なラッパークラス)。下のコードにはこのコンセプトの実例がありますが、コードが自明であることを願っています。

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

一般的な考え方は、あなたのニーズに応じてマーシャリングまたはマーシャリングすることができますあなたのリスト(または配列)のラッパーとして働くクラスを作ることです。

関連する問題