XJC, Java SE 6 ile başlayarak JDK'daki bin dizinine dahil edilmiştir. Örnek için bakınız:
Blogun içeriği aşağıdaki gibidir:
JAXB
Atom ile Atom Beslemelerini İşleme Atom , web beslemelerini temsil etmek için bir XML formatıdır. Standart bir format, okuyucu uygulamalarının farklı kaynaklardan beslemeleri görüntülemesine izin verir. Bu örnekte, bu blog için Atom beslemesini işleyeceğiz.
gösteri
Bu örnekte, bu bloga karşılık gelen Atom XML beslemesini nesnelere ve ardından tekrar XML'e dönüştürmek için JAXB'yi kullanacağız.
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.w3._2005.atom.FeedType;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("org.w3._2005.atom");
Unmarshaller unmarshaller = jc.createUnmarshaller();
URL url = new URL("http://bdoughan.blogspot.com/atom.xml");
InputStream xml = url.openStream();
JAXBElement<feedtype> feed = unmarshaller.unmarshal(new StreamSource(xml), FeedType.class);
xml.close();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(feed, System.out);
}
}
JAXB Modeli
Aşağıdaki model, Java derleyicisine (XJC) yönelik şema tarafından oluşturulmuştur. Yer kazanmak için get / set yöntemlerini ve yorumları atladım.
xjc -d generated http://www.kbcafe.com/rss/atom.xsd.xml
paket-info
@XmlSchema(
namespace = "http://www.w3.org/2005/Atom",
elementFormDefault = XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
package org.w3._2005.atom;
import javax.xml.bind.annotation.*;
CategoryType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "categoryType")
public class CategoryType {
@XmlAttribute(required = true)
protected String term;
@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String scheme;
@XmlAttribute
protected String label;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
İçerik türü
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "contentType", propOrder = {"content"})
public class ContentType {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
@XmlAttribute
protected String type;
@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String src;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
DateTimeType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
@XmlType(name = "dateTimeType", propOrder = {"value"})
public class DateTimeType {
@XmlValue
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar value;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
EntryType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "entryType", propOrder = {"authorOrCategoryOrContent"})
public class EntryType {
@XmlElementRefs({
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "summary", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "source", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "content", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "published", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContent;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
Yayın Türü
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "feedType", propOrder = {"authorOrCategoryOrContributor"})
public class FeedType {
@XmlElementRefs({
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "entry", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContributor;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
GeneratorType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "generatorType", propOrder = {"value"})
public class GeneratorType {
@XmlValue
protected String value;
@XmlAttribute
@XmlSchemaType(name = "anyURI")
protected String uri;
@XmlAttribute
protected String version;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
IconType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "iconType", propOrder = {"value"})
public class IconType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
IdType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "idType", propOrder = {"value"})
public class IdType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
linktype
package org.w3._2005.atom;
import java.math.BigInteger;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "linkType", propOrder = {"content"})
public class LinkType {
@XmlValue
protected String content;
@XmlAttribute(required = true)
@XmlSchemaType(name = "anyURI")
protected String href;
@XmlAttribute
protected String rel;
@XmlAttribute
protected String type;
@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NMTOKEN")
protected String hreflang;
@XmlAttribute
protected String title;
@XmlAttribute
@XmlSchemaType(name = "positiveInteger")
protected BigInteger length;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
logo
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "logoType", propOrder = {"value"})
public class LogoType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
PersonType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "personType", propOrder = {"nameOrUriOrEmail"})
public class PersonType {
@XmlElementRefs({
@XmlElementRef(name = "email", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "name", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "uri", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> nameOrUriOrEmail;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
Kaynak tipi
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "sourceType", propOrder = {"authorOrCategoryOrContributor"})
public class SourceType {
@XmlElementRefs({
@XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class),
@XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class)
})
@XmlAnyElement(lax = true)
protected List<Object> authorOrCategoryOrContributor;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
TextType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "textType", propOrder = {"content"})
public class TextType {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String type;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}
UriType
package org.w3._2005.atom;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.namespace.QName;
@XmlType(name = "uriType", propOrder = {"value"})
public class UriType {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlSchemaType(name = "anyURI")
protected String base;
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "language")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
}