simplexml_import_dom example


  protected function parseContent($content) {
    $htmlDom = new \DOMDocument();
    @$htmlDom->loadHTML('<?xml encoding="UTF-8">' . $content);
    $elements = simplexml_import_dom($htmlDom);

    return $elements;
  }

  /** * Performs an xpath search on a certain content. * * The search is relative to the root element of the $content variable. * * @param string $content * The html to parse. * @param string $xpath * The xpath string to use in the search. * @param array $arguments * Some arguments for the xpath. * * @return array|false * The return value of the xpath search. For details on the xpath string * format and return values see the SimpleXML documentation, * http://php.net/manual/function.simplexml-element-xpath.php. */
if ('2.0' === $xliffVersion) {
            $this->extractXliff2($dom$catalogue$domain);
        }
    }

    /** * Extract messages and metadata from DOMDocument into a MessageCatalogue. */
    private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void
    {
        $xml = simplexml_import_dom($dom);
        $encoding = $dom->encoding ? strtoupper($dom->encoding) : null;

        $namespace = 'urn:oasis:names:tc:xliff:document:1.2';
        $xml->registerXPathNamespace('xliff', $namespace);

        foreach ($xml->xpath('//xliff:file') as $file) {
            $fileAttributes = $file->attributes();

            $file->registerXPathNamespace('xliff', $namespace);

            foreach ($file->xpath('.//xliff:prop') as $prop) {
                

    protected function parseFile(string $path): \SimpleXMLElement
    {
        try {
            $dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');
        } catch (\Exception $e) {
            throw new MappingException($e->getMessage()$e->getCode()$e);
        }

        return simplexml_import_dom($dom);
    }

    private function loadClassesFromXml(): void
    {
        parent::__construct($this->file);

        // This method may throw an exception. Do not modify the class'         // state before it completes         $xml = $this->parseFile($this->file);

        $this->classes = [];

        
$constantValue = constant($constantName);

                        $dom->replaceChild($dom->ownerDocument->createTextNode($constantValue)$node);
                        break;

                    default:
                        throw new Zend_Config_Exception("Unknown node with name '$node->localName' found");
                }
            }

            return (string) simplexml_import_dom($dom);
        }

        // Search for children         if (count($xmlObject->children()) > 0) {
            foreach ($xmlObject->children() as $key => $value) {
                if (count($value->children()) > 0 || count($value->children(self::XML_NAMESPACE)) > 0) {
                    $value = $this->_toArray($value);
                } else if (count($value->attributes()) > 0) {
                    $attributes = $value->attributes();
                    if (isset($attributes['value'])) {
                        $value = (string) $attributes['value'];
                    }

  protected function parse() {
    if (!isset($this->elements)) {
      // DOM can load HTML soup. But, HTML soup can throw warnings, suppress       // them.       $html_dom = new \DOMDocument();
      @$html_dom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent(), LIBXML_NOBLANKS);
      if ($html_dom) {
        // It's much easier to work with simplexml than DOM, luckily enough         // we can just simply import our DOM tree.         $this->elements = simplexml_import_dom($html_dom);
      }
    }
    $this->assertNotFalse($this->elements, 'The current HTML page should be available for DOM navigation.');
    return $this->elements;
  }

  /** * Builds an XPath query. * * Builds an XPath query by replacing placeholders in the query by the value * of the arguments. * * XPath 1.0 (the version supported by libxml2, the underlying XML library * used by PHP) doesn't support any form of quotation. This function * simplifies the building of XPath expression. * * @param string $xpath * An XPath query, possibly with placeholders in the form ':name'. * @param array $args * An array of arguments with keys in the form ':name' matching the * placeholders in the query. The values may be either strings or numeric * values. * * @return string * An XPath query with arguments replaced. */
protected $elements;

  /** * Stores a view output in the elements. */
  public function storeViewPreview($output) {
    $html5 = new HTML5();
    $htmlDom = $html5->loadHTML('<html><body>' . $output . '</body></html>');
    if ($htmlDom) {
      // It's much easier to work with simplexml than DOM, luckily enough       // we can just simply import our DOM tree.       $this->elements = simplexml_import_dom($htmlDom);
    }
  }

}

    private function parseFile(string $file): \SimpleXMLElement
    {
        try {
            $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
        } catch (\Exception $e) {
            throw new MappingException($e->getMessage()$e->getCode()$e);
        }

        return simplexml_import_dom($dom);
    }

    private function getClassesFromXml(): array
    {
        $xml = $this->parseFile($this->file);
        $classes = [];

        foreach ($xml->class as $class) {
            $classes[(string) $class['name']] = $class;
        }

        


    /** @dataProvider getHtmlIdsTestData */
    public function testHtmlIds($css, array $elementsId)
    {
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $document = new \DOMDocument();
        $document->strictErrorChecking = false;
        $internalErrors = libxml_use_internal_errors(true);
        $document->loadHTMLFile(__DIR__.'/Fixtures/ids.html');
        $document = simplexml_import_dom($document);
        $elements = $document->xpath($translator->cssToXPath($css));
        $this->assertCount(\count($elementsId)$elements);
        foreach ($elements as $element) {
            if (null !== $element->attributes()->id) {
                $this->assertContains((string) $element->attributes()->id, $elementsId);
            }
        }
        libxml_clear_errors();
        libxml_use_internal_errors($internalErrors);
    }

    

  protected function getHtmlDom($output) {
    $html_dom = new \DOMDocument();
    @$html_dom->loadHTML($output);
    if ($html_dom) {
      // It's much easier to work with simplexml than DOM, luckily enough       // we can just simply import our DOM tree.       return simplexml_import_dom($html_dom);
    }
    return NULL;
  }

}

    protected function parseFile(string $path): \SimpleXMLElement
    {
        try {
            $dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');
        } catch (\Exception $e) {
            throw new MappingException($e->getMessage()$e->getCode()$e);
        }

        return simplexml_import_dom($dom);
    }

    private function loadClassesFromXml(): void
    {
        parent::__construct($this->file);

        // This method may throw an exception. Do not modify the class'         // state before it completes         $xml = $this->parseFile($this->file);

        $this->classes = [];

        

    private function parseFile(string $file): \SimpleXMLElement
    {
        try {
            $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
        } catch (\Exception $e) {
            throw new MappingException($e->getMessage()$e->getCode()$e);
        }

        return simplexml_import_dom($dom);
    }

    private function getClassesFromXml(): array
    {
        $xml = $this->parseFile($this->file);
        $classes = [];

        foreach ($xml->class as $class) {
            $classes[(string) $class['name']] = $class;
        }

        
if ( isset( $dom->doctype ) ) {
            return false;
        }

        foreach ( $dom->childNodes as $child ) {
            if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) {
                return false;
            }
        }

        $xml = simplexml_import_dom( $dom );
        if ( ! $xml ) {
            return false;
        }

        $return = new stdClass();
        foreach ( $xml as $key => $value ) {
            $return->$key = (string) $value;
        }

        return $return;
    }

    
Home | Imprint | This part of the site doesn't use cookies.