Prado\Xml\TXmlDocument::loadFromString PHP Méthode

loadFromString() public méthode

The version and encoding will be determined based on the parsing result.
public loadFromString ( $string ) : boolean
Résultat boolean whether the XML string is parsed successfully
    public function loadFromString($string)
    {
        // TODO: since PHP 5.1, we can get parsing errors and throw them as exception
        $doc = new \DOMDocument();
        if ($doc->loadXML($string) === false) {
            return false;
        }
        $this->setEncoding($doc->encoding);
        $this->setVersion($doc->xmlVersion);
        $element = $doc->documentElement;
        $this->setTagName($element->tagName);
        $this->setValue($element->nodeValue);
        $elements = $this->getElements();
        $attributes = $this->getAttributes();
        $elements->clear();
        $attributes->clear();
        static $bSimpleXml;
        if ($bSimpleXml === null) {
            $bSimpleXml = (bool) function_exists('simplexml_load_string');
        }
        if ($bSimpleXml) {
            $simpleDoc = simplexml_load_string($string);
            $docNamespaces = $simpleDoc->getDocNamespaces(false);
            $simpleDoc = null;
            foreach ($docNamespaces as $prefix => $uri) {
                if ($prefix === '') {
                    $attributes->add('xmlns', $uri);
                } else {
                    $attributes->add('xmlns:' . $prefix, $uri);
                }
            }
        }
        foreach ($element->attributes as $name => $attr) {
            $attributes->add(($attr->prefix === '' ? '' : $attr->prefix . ':') . $name, $attr->value);
        }
        foreach ($element->childNodes as $child) {
            if ($child instanceof \DOMElement) {
                $elements->add($this->buildElement($child));
            }
        }
        return true;
    }

Usage Example

Exemple #1
0
 public function testLoadFromString()
 {
     $xmlStr = '<?xml version="1.0" encoding="UTF-8"?><rootNode><node id="node1" param="attribute1"/><node id="node2" param="attribute2"/></rootNode>';
     $xmldoc = new TXmlDocument();
     self::assertTrue($xmldoc->loadFromString($xmlStr));
     self::assertEquals('1.0', $xmldoc->getVersion());
     self::assertEquals('UTF-8', $xmldoc->getEncoding());
 }
All Usage Examples Of Prado\Xml\TXmlDocument::loadFromString