PHP Handbuch
Zurück |  | Weiter

# CXXXII. SDO XML Data Access Service Functions

# Einführung

Warnung
Diese Erweiterung ist EXPERIMENTELL. Das Verhalten dieser Erweiterung, einschließlich der Funktionsnamen, und alles Andere was hier dokumentiert ist, kann sich in zukünftigen PHP-Versionen ohne Ankündigung ändern. Seien Sie gewarnt und verwenden Sie diese Erweiterung auf eigenes Risiko.

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, which can be either a file or a URL. In order to do this it needs to be told the location of the XML schema, which is passed as a parameter to the create method of the XML DAS. The schema 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.

# Anforderungen

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 the details of 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.

# Beispiele

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`. These two files are reproduced here:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:letter="http://letterSchema"
      targetNamespace="http://letterSchema">
      <xsd:element name="letters" type="letter:FormLetter"/>
      <xsd:complexType name="FormLetter" mixed="true">
        <xsd:sequence>
          <xsd:element name="date" minOccurs="0" type="xsd:string"/>
          <xsd:element name="firstName" minOccurs="0" type="xsd:string"/>
          <xsd:element name="lastName" minOccurs="0" type="xsd:string"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:schema>

    <letter:letters xmlns:letter="http://letterSchema">
      <date>March 1, 2005</date>
      Mutual of Omaha
      Wild Kingdom, USA
      Dear
      <firstName>Casy</firstName>
      <lastName>Crocodile</lastName>
      Please buy more shark repellent.
      Your premium is past due.
    </letter:letters>

Beispiel 1. Loading, altering, and saving an XML document

The following example shows how an XML document can be loaded from a file, altered, and written back.

`<?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");
}
?>`

An instance of the XML DAS is first obtained from the SDO_DAS_XML::create() method, which is a static method of the SDO_DAS_XML class. The location of the xsd is passed as a parameter. Once we have an instance of the XML DAS initialised with a given schema, we can use it to load the instance document using the loadFromFile() method. There is also a loadFromString() method if you want to load an XML instance document from a string. If the instance document loads successfully, you will be returned an object of type SDO_DAS_XML_Document, on which you can call the getRootDataObject() method to get the SDO data object which is the root of the SDO data graph. You can then use SDO operations to change the graph. In this example we alter the date, firstName, and lastName properties. Then we use the saveDocumentToFile() method to write the changed document back to the file system.

This will write the following to `letter-out.xml`.

    <?xml version="1.0" encoding="UTF-8"?>
    <FormLetter xmlns="http://letterSchema" xsi:type="FormLetter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <date>September 03, 2004</date>
      Mutual of Omaha
      Wild Kingdom, USA
      Dear
      <firstName>Anantoju</firstName>
      <lastName>Madhu</lastName>
      Please buy more shark repellent.
      Your premium is past due.
    </FormLetter>

Beispiel 2. Creating a new XML document

The previous example loaded the document from a file. This example shows how to create an SDO data graph in memory. In this example it is then saved to an XML string. Furthermore, because the letter contains both structured and unstructured content, it uses the Sequence API as well assignments to properties to construct the data graph.

`<?php
try {
$xmldas = SDO_DAS_XML::create("letter.xsd");
try {
$letter = $xmldas->createDataObject("http://letterSchema", "FormLetter");
$letter_seq = $letter->getSequence();
$letter_seq->insert("April 09, 2005", NULL, 'date');
$letter_seq->insert("Acme Inc. ", NULL, NULL);
$letter_seq->insert("United Kingdom. ");
$letter_seq->insert("Dear", NULL, NULL);
$letter_seq->insert("Tarun", NULL, "firstName");
$letter_seq->insert("Nayaraaa", NULL, "lastName");
$letter->lastName = "Nayar";
$letter_seq->insert("Please note that your order number ");
$letter_seq->insert(12345);
$letter_seq->insert(" has been dispatched today. Thanks for your business with us.");
print($xmldas->saveDataObjectToString($letter, "http://letterSchema", "FormLetter"));
} 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");
}
?>`

This will emit the following output (line breaks have been inserted for readability):

    <?xml version="1.0" encoding="UTF-8"?>
    <FormLetter xmlns="http://letterSchema" xsi:type="FormLetter"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <date>April 09, 2005</date>
    Acme Inc. United Kingdom.
    Dear
    <firstName>Tarun</firstName>
    <lastName>Nayar</lastName>
    Please note that your order number 12345 has been
    dispatched today. Thanks for your business with us.
    </FormLetter>

Beispiel 3. Setting XML document properties

This third example shows you how to use the SDO_DAS_XML_Document class. SDO_DAS_XML_Document class is provided to get and set properties of the XML declaration such as version, schema location, encoding and so on.

`<?php
try {
$xmldas = SDO_DAS_XML::create("letter.xsd");
$xdoc = $xmldas->loadFromFile("letter.xml");
print("Encoding is set to : " . $xdoc->getEncoding() . "\n");
print("XML Version : " . $xdoc->getXMLVersion() . "\n");
$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");
}
?>`

The first three lines of output show how the encoding and XML version has been obtained from the document, and how the XML version has been set in the XML header.

    Encoding is set to : UTF-8
    XML Version : 1.0
    <?xml version="1.1" encoding="UTF-8"?>
    ...

# Vordefinierte Klassen

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.

### Methoden

- create This is the only static method available in SDO_DAS_XML class. Used to construct SDO_DAS_XML object.

- createDataObject Can be used to construct the DataObject of a given type.

- loadFromFile Loads the xml instance document from a file. This file can be at local file system or it can be on a remote host.

- loadFromString same as the above method. Loads the xml instance which is available as string.

- saveDataObjectToFile save SDO_DataObject to a file.

- saveDataObjectToString save SDO_DataObject to a string.

- saveDocumentToFile save SDO_DAS_XML_Document object as a xml file.

- saveDocumentToString save SDO_DAS_XML_Document object as a xml string.

## SDO_DAS_XML_Document

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

### Methoden

- getEncoding gets the encoding string.

- getNoNamespaceSchemaLocation gets the no namespace schema location.

- getRootDataObject gets the root DataObject.

- getRootElementName gets the root DataObject's name.

- getRootElementURI gets the root DataObject's URI.

- getSchemaLocation gets schema location.

- getXMLDeclaration gets whether xml declaration is set ot not.

- getXMLVersion gets the xml version.

- setEncoding sets the encoding string with the given value.

- setNoNamespaceSchemaLocation sets the no namespace schema location with the given value.

- setSchemaLocation sets the schema location with the given value.

- setXMLDeclaraion to set/unset the xml declaration.

- setXMLVersion sets the xml version with the given value.

## SDO_DAS_XML_ParserException

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

Zurück | Zum Anfang | Weiter
SDO_Sequence::move | Nach oben | SDO_DAS_XML_Document::getEncoding