How to load schema, set properties and output string without SDO?

After struggling with installing SDO on the server, I found some information that SDO will not develop and be supported.

How can this be done without SDO?

$das = SDO_DAS_XML::create("$someSchemaFile");
$doc = $das->createDocument();
$root = $doc->getRootDataObject();
$root->Data1 = 'data1';
$root->Data2 = 'data2';
$string = $das->saveString($doc);

      

Scheme (pseudo)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cc="http://cc/XMLSchema">
    <xsd:element name="SomeName">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Data1" type="xsd:string"/>
                <xsd:element name="Data2" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

      

+3


source to share


1 answer


XSD is XML itself, so you have several ways to do this, such as DomDocument . But the simplest way will probably be SimpleXML , it's not that strong, but in most cases you don't need it.

Here's a small example:



$xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:cc="http://cc/XMLSchema">
    <xsd:element name="SomeName">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Data1" type="xsd:string"/>
                <xsd:element name="Data2" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element> </xsd:schema> 
XML;

$doc = simplexml_load_string($xml);

// get the first xsd:element node with a name-attribute of 'Data1' 
$element = $doc->xpath("//xsd:element[@name='Data1']")[0];

// change the name-attribute: 
$element->attributes()->name = 'SomeOtherName';

// or even add another attribute: 
$element->addAttribute('newAttribute', 'newAttributeValue');

// and spit it out as XML again:
echo $doc->asXML();

      

Hope this helps, as I'm not really sure what you mean. But as far as I understood your question, you are just looking for an alternative / easy way to manipulate the XML file.

+2


source







All Articles