到這個時候你可能已經解決了這個問題,這是一個有趣的練習,我有一些空閒時間。
在包含com.quick的包中創建一個名爲StepByStep的類,在coninue之前刪除所有IDE生成的代碼(清空文件)。
首先添加的軟件包再次
package com.quick;
首先增加進口
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.Element;
現在,添加一些補課
class Player {List<Property> property = new ArrayList<>();}
class Property {
String id, name, value;
public Property(String id, String name, String value) {
this.id = id;
this.name = name;
this.value = value;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // or write the get and set methods
class Players {
@XmlJavaTypeAdapter(PlayerTypeAdapter.class) // dont worry about this line
List<Player> player;
}
現在,添加XmlAdapter類(PlayerTypeAdapter) 這是最重要的部分,如果你已經知道@XmlJavaTypeAdapter如何工作KS和你只想知道如何把任意XML元素內重點說說
class PlayerTypeAdapter extends XmlAdapter<Object, Player> {
private DocumentBuilder documentBuilder;
public PlayerTypeAdapter() {
try {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (Exception e) {
}
}
@Override
public Player unmarshal(Object v) throws Exception {
Player p = new Player();
NodeList c = ((Element) v).getChildNodes();
for (int i = 0; i < c.getLength(); i++) {
Node n = c.item(i);
if (!(n instanceof Element)) continue;
Element e = (Element) n;
p.property.add(new Property(e.getAttribute("id"),
e.getTagName(), e.getTextContent()));
}
return p;
}
@Override
public Object marshal(Player v) throws Exception {
Document document = documentBuilder.newDocument();
Element root = document.createElement("dummy");
if (v.property != null) for (Property p : v.property) {
Element propertyNode = document.createElement(p.name);
propertyNode.setAttribute("id", p.id);
propertyNode.setTextContent(p.value);
root.appendChild(propertyNode);
}
return root;
}
}
最後,其主要方法加上分步類(只是爲了測試我們的代碼)
public class StepByStep {
public static void main(String[] args) throws JAXBException {
// context, marshaller and unmarshaller
JAXBContext context = JAXBContext.newInstance(Players.class, Player.class, Property.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Unmarshaller unmarshaller = context.createUnmarshaller();
// lets fill dummy players
Players p = new Players();
p.player = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Player e = new Player();
e.property.add(new Property("001", "FirstName", "Mahendra"));
e.property.add(new Property("002", "MiddleName", "Sing"));
e.property.add(new Property("003", "LastName", "Dhoni"));
p.player.add(e);
}
// marshal p (original)
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
marshaller.marshal(p, os1);
byte[] ba1 = os1.toByteArray();
Players q = (Players) unmarshaller.unmarshal(new ByteArrayInputStream(ba1));
// marshal q (copy)
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
marshaller.marshal(q, os2);
byte[] ba2 = os2.toByteArray();
// both q and p should be the same
System.out.println(new String(ba1));
System.out.println(new String(ba2));
}
}
可以爲元素和屬性定義自定義註釋,這些元素和屬性也可用於序列化Xmls。 – ManthanB