📄 sdo-das-xml.examples.html
字号:
<div class="example-contents"><p> 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 <a href="sdo.sample.sequence.html" class="link">the Sequence API</a> as well assignments to properties to construct the data graph. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Create an XML document from scratch<br /> */<br /></span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /> try {<br /> </span><span style="color: #0000BB">$doc </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">createDocument</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$rdo </span><span style="color: #007700">= </span><span style="color: #0000BB">$doc</span><span style="color: #007700">-></span><span style="color: #0000BB">getRootDataObject</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$seq </span><span style="color: #007700">= </span><span style="color: #0000BB">$rdo</span><span style="color: #007700">-></span><span style="color: #0000BB">getSequence</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"April 09, 2005"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">'date'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Acme Inc. "</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"United Kingdom. "</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Dear"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Tarun"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">"firstName"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Nayaraaa"</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">"lastName"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$rdo</span><span style="color: #007700">-></span><span style="color: #0000BB">lastName </span><span style="color: #007700">= </span><span style="color: #DD0000">"Nayar"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">"Please note that your order number "</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">12345</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$seq</span><span style="color: #007700">-></span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">" has been dispatched today. Thanks for your business with us."</span><span style="color: #007700">);<br /> print(</span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveString</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">));<br /> } catch (</span><span style="color: #0000BB">SDO_Exception $e</span><span style="color: #007700">) {<br /> print(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br /> }<br />} catch (</span><span style="color: #0000BB">SDO_Exception $e</span><span style="color: #007700">) {<br /> print(</span><span style="color: #DD0000">"Problem creating an XML document: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> The <b>createDocument()</b> method on the XML DAS returns a document object with a single root data object corresponding to an empty document element. The element name of the document element is known from the schema file. If there is any ambiguity about what the document element is, as there can be when more than one schema has been loaded into the same XML DAS, the element name and the namespace URI can be passed to the <b>createDocument()</b> method. </p> <p class="para"> This will emit the following output (line breaks have been inserted for readability): </p> <div class="example-contents"> <div class="cdata"><pre><?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></pre></div> </div> <div class="example"> <p><b>Example #3 Setting XML document properties</b></p> <div class="example-contents"><p> This third example shows you how to set the XML version and encoding on the document object. These will be used when the XML is written out. If no XML declaration is wanted at all (perhaps you want to generate the XML as a string to embed in something) then you can use the <b>setXMLDeclaration()</b> method to suppress it. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * Illustrate the calls that control the XML declaration<br /> */<br /> </span><span style="color: #0000BB">$xmldas </span><span style="color: #007700">= </span><span style="color: #0000BB">SDO_DAS_XML</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xsd"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document </span><span style="color: #007700">= </span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">loadFile</span><span style="color: #007700">(</span><span style="color: #DD0000">"letter.xml"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">setXMLVersion</span><span style="color: #007700">(</span><span style="color: #DD0000">"1.1"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$document</span><span style="color: #007700">-></span><span style="color: #0000BB">setEncoding</span><span style="color: #007700">(</span><span style="color: #DD0000">"ISO-8859-1"</span><span style="color: #007700">);<br /> print(</span><span style="color: #0000BB">$xmldas</span><span style="color: #007700">-></span><span style="color: #0000BB">saveString</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> The XML version and encoding are set in the XML declaration at the top of the XML document. </p></div> <div class="example-contents"><div class="cdata"><pre><?xml version="1.1" encoding="ISO-8859-1"?>.../...</pre></div> </div> </div> <div class="example"> <p><b>Example #4 Using an open type</b></p> <div class="example-contents"><p> This fourth example illustrates the use of an SDO open type and the use of the <b>createDataObject()</b> method. For this example we use the following two schema: </p></div> <div class="example-contents"><p> <div class="example-contents"><div class="cdata"><pre><schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name="jungle"> <complexType> <sequence> <any minOccurs="0" maxOccurs="unbounded"/> </sequence> </complexType> </element> </schema></pre></div> </div> </p></div> <div class="example-contents"><p> Note the presence of the <var class="varname">any</var> element in the definition. This first schema defines the <var class="varname">jungle</var> complex type as containing a sequence of any other type. The other types that the example will use are defined in a second schema file: </p></div> <div class="example-contents"><p> <div class="example-contents"><div class="cdata"><pre><schema xmlns= "http://www.w3.org/2001/XMLSchema"> <complexType name="snakeType"> <sequence>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -