Главная  ■  Статьи  ■  Examples
Поиск

Examples

Several of 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>

Example 2089. 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
/**
* Load, update, and save an XML document
*/
try {
 
$xmldas = SDO_DAS_XML::create("letter.xsd");
 
$document = $xmldas->loadFile("letter.xml");
 
$root_data_object = $document->getRootDataObject();
 
$root_data_object->date = "September 03, 2004";
 
$root_data_object->firstName = "Anantoju";
 
$root_data_object->lastName = "Madhu";
 
$xmldas->saveFile($document, "letter-out.xml");
  echo
"New file has been written:\n";
  print
file_get_contents("letter-out.xml");
} catch (
SDO_Exception $e) {
  print(
$e->getMessage());
}
?>

 

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 loadFile() method. There is also a loadString() 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 saveFile() method to write the changed document back to the file system. The saveFile method has an optional extra integer argument which if specified will cause the XML DAS to format the XML, using the integer as the amount to indent by at each change in level on the document.

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

Главная Статьи Карта сайта Ссылки Контакты

Rambler's Top100
Все права принадлежат www.netfuncards.ru . При копировании и размещении материала, ссылка на источник обязательна.
<a href="/">Виртуальные открытки</a>