Apache XMLBeans

from Wikipedia, the free encyclopedia
Apache XMLBeans

logo
Basic data

Maintainer David Bau, Cesar Andrei, Patrick Calahan et al.
developer Apache Software Foundation
Current  version 3.1.0
(March 26, 2019)
operating system cross-platform
programming language Java (programming language)
category XML data binding
License Apache license 2.0
xmlbeans.apache.org

XMLBeans is a software package for Java that enables data from an XML schema to be automatically linked to Java classes. This process is called XML data binding . This enables working with XML documents without the programmer having to use interfaces for processing XML such as SAX or DOM . XMLBeans is part of the Apache XML project.

functionality

Apache XMLBeans uses an XML schema definition to generate Java classes and interfaces. If the XML schema is not available, Apache XMLBeans can generate a schema based on an XML document.

The Java classes and interfaces generated by Apache XMLBeans can - as is common with JavaBeans - be processed using getter and setter methods . The rest of the XML information, such as the order of the elements in the XML document, can also be accessed from Java. XMLBeans masters all of the capabilities of XML schemas.

In addition to the development tools for generating XML documents and Java classes, Apache XMLBeans offers the following three programming interfaces :

XmlObject
The Java classes generated from the XML schema are derived from XmlObject. They contain strictly typed getter and setter methods for all elements defined in the XML document. Complex XML types are references to other Xml objects. XmlObject itself offers methods to validate the object (including its references), to search through it or to convert it into an XML.
XmlCursor
Represents a position in the XML document and offers the option of accessing the XML infoset. With the help of the XmlCursors it is possible, for example, to apply XQuery and XPath to the XML document, to change the XML document itself by inserting, moving, copying or deleting XML elements, or to entire container elements, attributes, processing instructions or Access comments.
SchemaType
Represents the XML schema behind the XmlObjects as an object. This allows, for example, XML documents to be generated or the sequence of elements to be read out.

example

The following example is based on an XML schema definition for the description of a country:

 <?xml version="1.0" encoding="UTF-8"?>
 <xs:schema targetNamespace="http://www.openuri.org/domain/country/v1"
            xmlns:tns="http://www.openuri.org/domain/country/v1"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified"
            version="1.0">
   <xs:element name="Country" type="tns:Country"/>
   <xs:complexType name="Country">
     <xs:sequence>
       <xs:element name="Name" type="xs:string"/>
       <xs:element name="Population" type="xs:int"/>
       <xs:element name="Iso" type="tns:Iso"/>
     </xs:sequence>
   </xs:complexType>
   <xs:complexType name="Iso">
     <xs:annotation><xs:documentation>ISO 3166</xs:documentation></xs:annotation>
     <xs:sequence>
       <xs:element name="Alpha2" type="tns:IsoAlpha2"/>
       <xs:element name="Alpha3" type="tns:IsoAlpha3"/>
       <xs:element name="CountryCode" type="tns:IsoCountryCode"/>
     </xs:sequence>
   </xs:complexType>
   <xs:simpleType name="IsoCountryCode">
     <xs:restriction base="xs:int">
       <xs:totalDigits value="3"/>
     </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="IsoAlpha2">
     <xs:restriction base="xs:string">
       <xs:pattern value="[A-Z]{2}"/>
       <xs:whiteSpace value="collapse"/>
     </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="IsoAlpha3">
     <xs:restriction base="xs:string">
       <xs:pattern value="[A-Z]{3}"/>
       <xs:whiteSpace value="collapse"/>
     </xs:restriction>
   </xs:simpleType>
 </xs:schema>

From this schema, Java classes can now be created with the XMLBeans tools scomp (schema compiler) or xmlbeans (an Ant task), which can generate and manipulate XML data corresponding to the given schema. The following Java code shows how an XML document can be generated and validated with it:

 import org.openuri.domain.country.v1.Country; //die aus dem XML Schema erzeugte Country Klasse
 import org.openuri.domain.country.v1.Iso; //die aus dem XML Schema erzeugte Iso Klasse
 public class CountrySample {
   public static void main(String[] args) {
     Country country = Country.Factory.newInstance();
     country.setName("Liechtenstein");
     country.setPopulation(35446); // siehe http://de.wikipedia.org/wiki/Liechtenstein

     // XML-Ausgabe des Country-Objektes
     System.out.println(country.xmlText());

     // Prüfung, ob das Country-Objekt valide ist (ist invalid, weil es keine Iso hat)
     System.out.println ("Country ist " + (country.validate() ? "valid" : "invalid"));

     // Füge den komplexen Typ Iso dem Country-Objekt hinzu
     Iso iso = country.addNewIso();
     iso.setAlpha2("LI"); // siehe http://de.wikipedia.org/wiki/ISO-3166-1-Kodierliste
     iso.setAlpha3("LIE");
     iso.setCountryCode(438);

     // XML-Ausgabe des Country-Objektes
     System.out.println(country.xmlText());

     // Prüfung, ob das Country-Objekt valide ist (ist jetzt valide)
     System.out.println ("Document is " + (country.validate() ? "valid" : "invalid"));
   }
 }

The classes Country and Iso generated from the XML schema can also be org.apache.xmlbeans.XmlBeans.compileXsdgenerated dynamically at runtime.

history

Apache XMLBeans was founded in 2003 under the name XMLBeans by David Bau when he was working for BEA Systems . It was based on XMLMaps, the earlier XML binding framework of the BEA WebLogic server. XMLBeans was originally a proprietary component of the “BEA WebLogic Workshop” framework. On January 27, 2003, it was first presented as a preview version. On September 24th of the same year, BEA donated XMLBeans to the Apache Software Foundation, where it was accepted as an Apache Incubator project.

On April 23, 2004, version 1.0.2 was the first version of Apache to be completed. On June 25th of the same year, XMLBeans became an Apache top-level project. Version 2.0 was released on June 30th. Among other things, it contained the following improvements: Improved integration of XQuery and XPath , support for DOM Level 2, improved error handling, options for extending the generated XMLBeans, performance improvements, support for the generics published with Java 5, generation of XML documents from XML schemas and vice versa.

The current version is Apache XMLBeans 2.6 from August 14, 2012. Among other things, it brought simple mapping of simple types to any Java classes as well as many small enhancements and bug fixes.

Further development was discontinued on May 23, 2014.

Alternatives

The following frameworks for XML binding in Java can be used as alternatives to Apache XMLBeans:

Web links

Individual evidence

  1. XMLBeans Committers XMLBeans Community
  2. a b XMLBeans News