CXVIII. SDO XML Data Access Service Functions

Introduktion

Varning

Den h�r utbyggnaden �r EXPERIMENTELL. Dess beteende -- inkluderat namnen p� funktionerna, och allt annat dokumenterat om den h�r utbyggnaden -- kan �ndras i framtida utg�var av PHP utan n�got tillk�nnagivande. Anv�nd den p� egen risk.

In order to use the XML Data Access Service for Service Data Objects, you will need to understand some of the concepts behind SDO: the data graph, the data object, XPath and property expressions, and so on. If you are not familiar with these ideas, you might want to look first at the section on SDO .

The job of the XML DAS is to move data between the application and an XML data source. In order to do this it needs to be told the XML schema the XML should follow. This model information is used to ensure conformance of XML being written out, and also to ensure that modifications made to an SDO originating from XML follow the model described by the XML schema.

The XML DAS currently supports reading/writing XML to/from a file or an http URL (for example, to support RSS feeds).

Krav

The SDO XML Data Access Service requires PHP 5.1 or higher. It is packaged with the SDO extension and requires SDO to have been installed. See the SDO installation instructions for steps on how to do this.

Installation

The XML Data Access Service is packaged and installed as part of the SDO extension. Please Refer SDO installation instructions.

Limitations

The SDO 2.0 specification defines the mapping between XML types and SDO types. With Java SDO, this mapping is implemented by the XMLHelper. With SDO for PHP, this mapping is implemented by the XML Data Access Services. The XML DAS implements the mapping described in the SDO 2.0 specification with the following restrictions:

XML Simple Types

  1. Simple Type with abstract="true" - no PHP support for SDO abstract types.

  2. Simple Type with sdoJava:instanceClass - no PHP equivalent provided.

  3. Simple Type with sdoJava:extendedInstanceClass - no PHP equivalent provided.

  4. Simple Type with list of itemType.

  5. Simple Type with union.

XML Complex Types

  1. Complex Type with abstract="true" - no PHP support for SDO abstract types.

  2. Complex Type with sdo:aliasName - no PHP support for SDO Type aliases.

  3. Complex Type with open content - no PHP support for SDO open types.

  4. Complex Type with open attribute - no PHP support for SDO open types.

XSD Attribute

  1. Attribute with sdo:aliasName - no PHP support for SDO property aliases.

  2. Attribute with default value - no PHP support for SDO property defaults.

  3. Attribute with fixed value - no PHP support for SDO read-only properties or default values.

  4. Attribute reference.

  5. Attribute with sdo:string - no support for sdo:string="true".

  6. Attribute referencing a DataObject with sdo:propertyType - no support for sdo:propertyType="...".

  7. Attribute with bi-directional property to a DataObject with sdo:oppositeProperty and sdo:propertyType - no PHP support for SDO opposite.

XSD Elements

  1. Element with sdo:aliasName - no PHP support for SDO property aliases.

  2. Element reference.

  3. Element with nillable.

  4. Element with substitution group.

XSD Elements with Simple Type

  1. Element of SimpleType with default - no PHP support for SDO defaults

  2. Element of SimpleType with fixed value - no PHP support for SDO read-only properties or default values.

  3. Element of SimpleType with sdo:string - no support for sdo:string="true".

  4. Element referencing a DataObject with sdo:propertyType - no support for sdo:propertyType="..."

  5. Element with bi-directional reference to a DataObject with sdo:oppositeProperty and sdo:propertyType - no PHP support for SDO opposite.

Exempel

The following examples are based on the letter example described in the SDO documentation . The examples assume the XML Schema for the letter is contained in a file letter.xsd and the letter instance is in the file letter.xml .

Exempel 1. Working with the SDO_DAS_XML Class

The following example shows how to create a SDO_DAS_XML Object and use it to load an instance document. The SDO_DAS_XML object can be created by passing the xsd file to the SDO_DAS_XML::create method, which is a static method of SDO_DAS_XML Class. This schema file(XSD Document) can be either file available on the local file system or it can be an URL. Once we have created the SDO_DAS_XML Object, we can use the same to load the instance document using loadFromFile method. loadFromString method can be used if want load xml instance document which is available as string. On successful loading of the instance document, you will be returned with an object of type SDO_DAS_XML_Document. Use the getRootDataObject method of SDO_DAS_XML_Document class to get the root DataObject. This example also tries to modify the properties of the root DataObject and then write the changed contents back to file system.

<?php
try
{
    
$xmldas = SDO_DAS_XML::create("letter.xsd");
    
$xdoc = $xmldas->loadFromFile("letter.xml");
    
$do = $xdoc->getRootDataObject();
    
$do->date = "September 03, 2004";
    
$do->firstName = "Anantoju";
    
$do->lastName = "Madhu";
    
$xmldas->saveDocumentToFile($xdoc, "letter-out.xml");
}
catch (SDO_TypeNotFoundException $e) {
    print(
"Type is not defined in the xsd file");
}
catch (SDO_DAS_XML_ParserException $e) {
    print(
"Problem while parsing");
}
?>

Exempel 2. Working with the SDO_DAS_XML Class

The above example shown, how to use the SDO_DAS_XML object to load an existing instance document and change the DataObject(property values) and write the changes back to file system. This example shows, how to create the DataObjects dynamically and save them as a xml string. Please note that this example uses a company schema(( company.xsd )) as defined in SDO samples section.

<?php
$xmldas
= SDO_DAS_XML::create("company.xsd");
$do = $xmldas->createDataObject("companyNS", "CompanyType");
$do->name = "Acme Inc";
$dept1 = $do->createDataObject("departments");
$dept1->name = "sales";
$emp1 = $dept1->createDataObject("employees");
$emp1->name = "Fred";
$emp1->manager = FALSE;
$dept2 = $do->createDataObject("departments");
$emp2 = $dept2->createDataObject("employees");
$emp2->name = "Neil";
$emp2->manager = TRUE;
print(
$xmldas->saveDataObjectToString($do, "companyNS", "CompanyType"));
var_dump($do);
?>

Exempel 3. Working with the SDO_DAS_XML Class

This example shows you how to create the DataObject and use the Sequence API ( For more information on Sequence API ) to construct a letter containing unstructured text.

<?php
try
{
    
$xmldas = SDO_DAS_XML::create("letter.xsd");
    
try {
        
$do = $xmldas->createDataObject("http://letterSchema", "FormLetter");
        
$seq = $do->getSequence();
        
$seq->insert("April 09, 2005", NULL, 'date');
        
$seq->insert("Acme Inc. ", NULL, NULL);
        
$seq->insert("United Kingdom. ");
        
$seq->insert("Dear", NULL, NULL);
        
$seq->insert("Tarun", NULL, "firstName");
        
$seq->insert("Nayaraaa", NULL, "lastName");
        
$do->lastName = "Nayar";
        
$seq->insert("Please note that your order number ");
        
$seq->insert(12345);
        
$seq->insert(" has been dispatched today. Thanks for your business with us.");
        
$type = $do->getType();
        print(
$xmldas->saveDataObjectToString($do, $type[0], $type[1]));
    }
catch (SDO_Exception $e) {
        print(
$e);
    }
}
catch (SDO_TypeNotFoundException $e) {
    print(
"Type is not defined in the xsd file");
}
catch (SDO_DAS_XML_ParserException $e) {
    print(
"Problem while parsing");
}
?>

Exempel 4. Working with the SDO_DAS_XML_Document Class

This example shows you how to use the SDO_DAS_XML_Document class. SDO_DAS_XML_Document class is provided to get/set XML declarations such as version, schema location, encoding etc.

<?php
try
{
    
$xmldas = SDO_DAS_XML::create("letter.xsd");
    
$xdoc = $xmldas->loadFromFile("letter.xml");
    print(
"Encoding is set to : " . $xdoc->getEncoding());
    print(
"XML Version : " . $xdoc->getXMLVersion();
    
$xdoc->setXMLVersion("1.1");
    print(
$xmldas->saveDocumentToString($xdoc));
}
catch (SDO_TypeNotFoundException $e) {
    print(
"Type is not defined in the xsd file");
}
catch (SDO_DAS_XML_ParserException $e) {
    print(
"Problem while parsing");
}
?>

F�rdefinerade klasser

The XML DAS provides three classes. The SDO_DAS_XML which is the main class used to fetch the data from the XML source and also can used to write the data back. The next one is SDO_DAS_XML_Document class, which is basically useful to get/set the XML declarations such as encoding, version etc. And the last class is SDO_DAS_XML_ParserException which will be thrown for any parser exceptions while loading xsd/xml file.

SDO_DAS_XML

This is the main class of XML DAS, which is used fetch the data from the xml source and also used to write the data back. Other than the methods to load and save xml files, it also has a method called createDataObject which can be used to create an empty DataObject of a given type.

Methods

SDO_DAS_XML_Document

This class can be used to get/set XML Declarations such as encoding, schema location etc.

Methods

SDO_DAS_XML_ParserException

Is a subclass of SDO_Exception . Thrown for any parser errors while loading the xsd/xml file.

Innehållsförteckning
SDO_DAS_XML_Document::getEncoding --  Returns encoding string.
SDO_DAS_XML_Document::getNoNamespaceSchemaLocation --  Returns no namespace schema location.
SDO_DAS_XML_Document::getRoorDataObject --  Returns the root SDO_DataObject.
SDO_DAS_XML_Document::getRootElementName --  Returns root element's name.
SDO_DAS_XML_Document::getRootElementURI --  Returns root element's URI string.
SDO_DAS_XML_Document::getSchemaLocation --  Returns schema location.
SDO_DAS_XML_Document::getXMLDeclaration --  Returns whether the xml declaration is set or not.
SDO_DAS_XML_Document::getXMLVersion --  Returns xml declaration string.
SDO_DAS_XML_Document::setEncoding --  Sets the given string as encoding.
SDO_DAS_XML_Document::setNoNamespaceSchemaLocation --  Sets the given string as no namespace schema location.
SDO_DAS_XML_Document::setSchemaLocation --  Sets the given string as schema location.
SDO_DAS_XML_Document::setXMLDeclaration --  Sets the xml declaration.
SDO_DAS_XML_Document::setXMLVersion --  Sets the given string as xml version.
SDO_DAS_XML::create --  To create SDO_DAS_XML object for a given schema file.
SDO_DAS_XML::createDataObject --  Creates SDO_DataObject for a given namespace URI and type name.
SDO_DAS_XML::loadFromFile --  Returns SDO_DAS_XML_Document object for a given path to xml instance document.
SDO_DAS_XML::loadFromString --  Returns SDO_DAS_XML_Document for a given xml instance string.
SDO_DAS_XML::saveDataObjectToFile --  Saves the SDO_DataObject object to File.
SDO_DAS_XML::saveDataObjectToString --  Saves the SDO_DataObject object to string.
SDO_DAS_XML::saveDocumentToFile --  Saves the SDO_DAS_XML_Document object to a file.
SDO_DAS_XML::saveDocumentToString --  Saves the SDO_DAS_XML_Document object to a string.