3

私はこのようなxmlファイルを持っています

<info>
  <item key=1>value1</item>
  <item key=2>value2</item>
</info>

そして、私はこのようなバインドされたクラスを取得したい

class Info {
    @XmlJavaTypeAdapter(MapAdapter.class)
    private Map<Integer,Item> map;

    public setMap...
    public getMap...
}

class Item{
    @XmlAttribute
    private Integer key;

    @XmlValue
    private String value;

    //get,set method...
}

ラップされたフィールドで楽しく動作します

<info>
  <map>
    <item key=1>value1</item>
    <item key=2>value2</item>
  </map>
</info>

を取り除くと<map>、エラーなしで失敗しました。MapAdapter が機能しませんでした。

public Map<Integer, Item> unmarshal(MapType myMapType) throws Exception {
    HashMap<Integer, Item> hashMap = new HashMap<Integer, Item>();
    for (Item myEntryType : myMapType.getEntry()) {
        hashMap.put(myEntryType.getKey(), myEntryType);
    }
    return hashMap;
}

myMapType は常に null になります。

この xml で何ができますか?

4

4 に答える 4

1

あなたInfoはのデコレータですMap。あなたの例では、それは地図上で価値を提供しません。私は2つの選択肢を見ます:

  1. を削除Infoし、上に移動mapしての使用を置き換えますinfo

  2. 地図の代わりにあなた@XmlJavaTypeAdapterのために書いてください。Info内部をマーシャリング/アンマーシャリングしmapます-すでに行っていることを、レベルを上げるだけです。

于 2012-08-14T16:03:19.840 に答える
1

私の解決策はそれです

  1. マップを @XmlTransient のままにします
  2. 別のプロパティを作成する

完全な mavenized プロジェクトはこちらhttp://code.google.com/p/jinahya/source/browse/trunk/com.googlecode.jinahya/stackoverflow

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Info {

    @XmlElement(name = "item")
    private List<Item> getItems() {
        return new ArrayList<Item>(getMap().values());
    }

    private void setItems(final List<Item> items) {
        getMap().clear();
        for (Item item : items) {
            getMap().put(item.getKey(), item);
        }
    }

    public Map<Integer, Item> getMap() {
        if (map == null) {
            map = new HashMap<Integer, Item>();
        }
        return map;
    }

    private Map<Integer, Item> map;
}

テスト

@Test
public void testXml() throws JAXBException {

    final JAXBContext context = JAXBContext.newInstance(Info.class);

    final Info marshall = new Info();
    marshall.getMap().put(1, Item.newInstance(1, "value1"));
    marshall.getMap().put(2, Item.newInstance(2, "value2"));

    final Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(marshall, baos);
    System.out.println(new String(baos.toByteArray()));

    final Unmarshaller unmarshaller = context.createUnmarshaller();

    final Info unmarshal = (Info) unmarshaller.unmarshal(
        new ByteArrayInputStream(baos.toByteArray()));

    for (Item item : unmarshal.getMap().values()) {
        System.out.println(item);
    }
}

版画

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<info>
    <item key="1">value1</item>
    <item key="2">value2</item>
</info>

key=1&value=value1
key=2&value=value2
于 2012-08-15T08:10:55.690 に答える
0

マップメンバーを識別します。マップメンバーの@XmlValueアノテーションが機能する可能性があります。

于 2012-08-14T16:03:30.657 に答える
0

注: 私はEclipseLink JAXB (MOXy)のリーダーであり、JAXB (JSR-222)エキスパート グループのメンバーです。

MOXy の@XmlPath拡張機能を活用して、ユース ケースをサポートできます。

情報

package forum11956071;

import java.util.Map;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Info {
    @XmlJavaTypeAdapter(MapAdapter.class)
    @XmlPath(".")
    private Map<Integer,String> map;

}

マップアダプター

package forum11956071;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<Integer, String>>{

    public static class AdaptedMap {
        public List<Item> item = new ArrayList<Item>();
    }

    public static class Item {
        @XmlAttribute Integer key;
        @XmlValue String value;
    }
    @Override
    public AdaptedMap marshal(Map<Integer, String> map) throws Exception {
        AdaptedMap adaptedMap = new AdaptedMap();
        for(Entry<Integer, String> entry : map.entrySet()) {
            Item item = new Item();
            item.key = entry.getKey();
            item.value = entry.getValue();
            adaptedMap.item.add(item);
        }
        return adaptedMap;
    }

    @Override
    public Map<Integer, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
        Map<Integer, String> map = new HashMap<Integer, String>();
        for(Item item : adaptedMap.item) {
            map.put(item.key, item.value);
        }
        return map;
    }

}

jaxb.properties

MOXy を JAXB プロバイダーとして指定するにはjaxb.properties、次のエントリを使用して、ドメイン モデルと同じパッケージに file という名前のファイルが必要です ( http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-を参照)。 as-your.html :

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

デモ

package forum11956071;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Info.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11956071/input.xml");
        Info info = (Info) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(info, System.out);
    }

}

input.xml/出力

<?xml version="1.0" encoding="UTF-8"?>
<info>
   <item key="1">value1</item>
   <item key="2">value2</item>
</info>
于 2012-08-14T21:06:23.547 に答える