RFID tags are increasingly used to track supply chains. This is accomplished by when tagged subjects move through tag readers. The readers send signals to a RFID middleware that collects this signals and send events in nicely formatted XML format. I refer these XML events as Epcis Events. They events conform to an XML schema, making it easy to decode them. XMLBeans provides a nice way to deal with XML elements via Java objects. In this article I will show how to use parse the events using XML Beans
We need the following
The following section in the ant file does the compiling
<xmlbean schema="schemas"
destfile="${schema.jarfile}"
srcgendir="build/src"
classpathref="xmlbeans.path"
debug="on"
/>This generated bunch of Java files from Schema files and put them in a JAR file.
Go into the project directory and invoke ANT, or in Eclipse
right
click on the build.xml file and say 'Run As --> Ant'.
You
would see something similar to the following output
As can be seen from the image above right, the generated Java Interfaces/Class hierarchy models the EpcisEvent hiearchy. Epcis standard defines four kinds of events
Explore type
hiearchy in Eclipse![]() | event
class hierarchy![]() |
With generated Java types, dealing with the event XML is lot easier. Lets get down to the Java code
// imports
import java.io.FileReader;
import java.util.Calendar;
import org.unece.cefact.namespaces.standardBusinessDocumentHeader.StandardBusinessDocumentHeader;
import epcglobalEpcisXsd1.*;
EPCISDocumentDocument1 epcisDoc = EPCISDocumentDocument1.Factory
.parse(new FileReader("events/all-events.xml"));
EPCISDocumentType docType = epcisDoc.getEPCISDocument();
// header
EPCISHeaderType header = docType.getEPCISHeader();
StandardBusinessDocumentHeader bizHeader = header.getStandardBusinessDocumentHeader();
String version = bizHeader.getHeaderVersion();
// events
// lets just get the first of each events
// and examine some of the attributes for each event
EPCISBodyType body = docType.getEPCISBody();
EventListType eventList = body.getEventList();
// --------- read events
// Aggregation Event
AggregationEventType aggEvent = eventList.getAggregationEventArray(0);
// event time and record time are available for all events
Calendar recordTime = aggEvent.getRecordTime();
Calendar eventTime = aggEvent.getEventTime();
String parentID = aggEvent.getParentID();
EPCListType childEpcs = aggEvent.getChildEPCs();
// Object Event
ObjectEventType objEvent = eventList.getObjectEventArray(0);
String action = objEvent.getAction().toString();
// Quantiy Event
QuantityEventType quanEvent = eventList.getQuantityEventArray()[0];
int quantity = quanEvent.getQuantity();
// Transaction event
TransactionEventType transEvent = eventList.getTransactionEventArray(0);
BusinessTransactionListType bizTrans = transEvent.getBizTransactionList();
Calendar now = Calendar.getInstance(); objEvent.setRecordTime(now);
(There
could be a small
confusion understanding EventTime and
RecordTime. EventTime is when the event actually
occured -
i.e. when the tagged subject passed through the reader creating the
event. EventTime doesn't change. RecordTime may be
used to
indicate when the event was recorded into an Epcis repository via an
application)
<?xml version="1.0" encoding="UTF-8"?>
<epcis:EPCISDocument xmlns:epcglobal="urn:epcglobal:xsd:1"
xmlns:epcis="urn:epcglobal:epcis:xsd:1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:foo="http://foo"
xmlns:bar="http://bar">
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime>2006-05-16T17:57:26Z</eventTime>
<!-- .... usual attributes .... -->
<extension>
<!-- standard extensions go here -->
<temperature>30.1C</temperature>
</extension>
<!-- vendor extensions follow, they are in their own namespace -->
<foo:newFOOExtension>Value_FOO</foo:newFOOExtension>
<bar:newBARExtension>
<bar:barhello>Bar Hello </bar:barhello>
</bar:newBARExtension>
</ObjectEvent>
</EventList>
</EPCISBody>
</epcis:EPCISDocument>
<xsd:complexType name="ObjectEventType">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Object Event captures information about an event pertaining to one or more
objects identified by EPCs.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="epcis:EPCISEventType">
<xsd:sequence>
<xsd:element name="epcList" type="epcis:EPCListType"/>
<xsd:element name="action" type="epcis:ActionType"/>
<xsd:element name="bizStep" type="epcis:BusinessStepIDType" minOccurs="0"/>
<xsd:element name="disposition" type="epcis:DispositionIDType" minOccurs="0"/>
<xsd:element name="readPoint" type="epcis:ReadPointType" minOccurs="0"/>
<xsd:element name="bizLocation" type="epcis:BusinessLocationType" minOccurs="0"/>
<xsd:element name="bizTransactionList" type="epcis:BusinessTransactionListType" minOccurs="0"/>
<xsd:element name="extension" type="epcis:ObjectEventExtensionType" minOccurs="0"/>
<xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute processContents="lax"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
// standard extension ObjectEventExtensionType ext = objEvent.getExtension(); // get the DOM Node Node extNode = ext.getDomNode(); // or get it as string String extStr = ext.toString(); // pretty output XmlOptions xmlopts = new XmlOptions(); xmlopts.setSaveOuter(); xmlopts.setSavePrettyPrint(); String extStr2 = ext.xmlText(xmlopts);

//Looking into extension // feed the extension to new Schema parser MyExtRoot.Factory.parse (new StringReader (extStr)); // or you can feed the DOM Node MyExtRoot.Factory.parse (extNode); // or any children of node MyExtRoot.Factory.parse (extNode.getFirstChild ());
<xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>Again, we can use use plain XML Parsing to process the content.
/// vendor extension // doing DOM processing, get the next element after 'extNode' Node n = extNode.getNextSibling(); while ( (n != null) && !(n instanceof Element)) n = n.getNextSibling(); // now 'n' has the first node of Vendor extension '<foo:newFOOExtension>' Node vendorExtNode = n;
// xmlbeans way
QNameSet qns = QNameSet.forWildcardNamespaceString("##other", "*");
// get them as xmlObjects
XmlObject[] newExts = objEvent.selectChildren(qns);
// make them into an string
StringBuffer buf = new StringBuffer();
// if we use TOSTRING then only 'xml-fragments' are printed out.
// use xmlText with XMLOptions to get full element printout
XmlOptions xmlopts2 = new XmlOptions();
xmlopts2.setSaveOuter();
xmlopts2.setSavePrettyPrint();
for (int i = 0; (newExts != null) && (i < newExts.length); i++) {
buf.append(newExts[i].xmlText(xmlopts2)).append("\n");
}
String vendorExtStr = buf.toString();We have looked at how to use XMLBeans to easily access (get and set) to contents of Epcis events. We have seen code snippets to access standard attributes and also extension attributes of events.