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;
}