At last month's Zend Conference I was fortunate enough to attend Christian Wenz's tutorial on XML and Web services. This covered the use of technologies such as DOM and SimpleXML for working with XML data. As the title of this blog entry suggests, SDO provide a simple way to construct or extend XML documents.
The example below shows an XML schema used by an application which records information regarding quotations people have made (Thanks go to Christian Wenz for the scenario which is from his book entitled, "PHP Phrasebook", SAMS Publishing.). An XML document following this schema will contain a quotes
element containing a number of quote
elements, each of which consists of a phrase
and an author
element and a year
<?xml version="1.0" encoding="UTF-8"?>
<xs:element maxOccurs="unbounded" ref="quote"/>
<xs:attribute name="year" use="required" type="xs:integer"/>
<xs:element name="phrase" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
The code extract below shows how SDO can be used to load the XML schema and a quotes
document and then add a new quote
entry to that document.
1 $xmldas = SDO_DAS_XML::create('./quotes.xsd');
2 $xdoc = $xmldas->loadFromFile('./quotes.xml');
3 $quotes = $xdoc->getRootDataObject();
4 $quote = $quotes->createDataObject('quote');
5 $quote->phrase = $_POST['quote'];
6 $quote->author = $_POST['author'];
7 $quote->year = $_POST['year'];
8 $xmldas->saveDocumentToFile($xdoc, './quotes.xml');
The eight lines of code above do the following:
1. create and configure a new XML Data Access Service (DAS) with an XML schema.
2. use the XML DAS to load and validate an instance document.
3. retrieve the quotes currently contained in the document.
4-7. create a new quote and populate it with information posted from a form.
8. save the quotes (including the new quote) back to the xml file.
The same task takes 14 lines of code in DOM and in my opinion, DOM code is far less intuitive. I found it more difficult to understand the data structure from the code, because of the generic XML vocabulary of the DOM APIs. However, it is worth noting that the XML Data Access Service requires the existence of an XML schema, whereas DOM does not.
Because SDO knows about the XML schema, it lets you access the elements and attributes directly by name. The same is true of SimpleXML, however SDO can also be used to create new documents from scratch, or in this case, new fragments of XML. SimpleXML is limited in this regard because it only has knowledge of an instance document, rather than the schema. Again, it is worth noting that SimpleXML does not require the existence of an XML schema.
The SDO API does not reveal which names correspond to XML elements and which to XML attributes, but still outputs XML which follows the provided XML schema. Personally, I think this is quite neat, because often the choice to use an element or attribute in schema design is arbitrary and down to personal taste. This article
nicely sums up the feelings I had when I created my first ever XML schema.Graham Charters