コード例 #1
1
 private function writeReaderImpl(XMLWriter $writer, XMLReader $reader)
 {
     switch ($reader->nodeType) {
         case XMLReader::ELEMENT:
             $writer->startElement($reader->name);
             if ($reader->moveToFirstAttribute()) {
                 do {
                     $writer->writeAttribute($reader->name, $reader->value);
                 } while ($reader->moveToNextAttribute());
                 $reader->moveToElement();
             }
             if ($reader->isEmptyElement) {
                 $writer->endElement();
             }
             break;
         case XMLReader::END_ELEMENT:
             $writer->endElement();
             break;
         case XMLReader::COMMENT:
             $writer->writeComment($reader->value);
             break;
         case XMLReader::SIGNIFICANT_WHITESPACE:
         case XMLReader::TEXT:
             $writer->text($reader->value);
             break;
         case XMLReader::PI:
             $writer->writePi($reader->name, $reader->value);
             break;
         default:
             XMLReaderNode::dump($reader);
     }
 }
コード例 #2
0
 /**
  * @test
  * @dataProvider provideAllFiles
  *
  * @param string $xml
  */
 function nextBehavior($xml)
 {
     $reader = new XMLReaderStub($xml);
     $it = new XMLReaderNextIteration($reader);
     $expected = array();
     while ($reader->next()) {
         $expected[] = XMLReaderNode::dump($reader, true);
     }
     $reader->rewind();
     $index = 0;
     foreach ($it as $index => $node) {
         $this->assertEquals($expected[$index], XMLReaderNode::dump($reader, true));
     }
     $this->assertCount($index + 1, $expected);
 }
コード例 #3
0
/*
 * This file is part of the XMLReaderIterator package.
 *
 * Copyright (C) 2014 hakre <http://hakre.wordpress.com>
 *
 * Example: XML Reader standard iteration
 */
require 'xmlreader-iterators.php';
// require XMLReaderIterator library
$xml = <<<XML
<movies>
    <movie>
        <title>PHP: Behind the Parser</title>
    </movie>
    <movie>
        <title>Whitespace</title>
    </movie>
    <movie>
        <title>Whitespace - Tabs Revenge</title>
    </movie>
    <movie>
        <title>Whitespace - The Return of the Whitespace</title>
    </movie>
</movies>
XML;
$reader = new XMLReader();
$reader->open('data://text/plain,' . urlencode($xml));
while ($reader->read()) {
    XMLReaderNode::dump($reader);
}
コード例 #4
0
 private function build()
 {
     if (!$this->valid()) {
         $this->depth = NULL;
         $this->lastDetpth = NULL;
         $this->node = NULL;
         $this->lastNode = NULL;
         $this->stack = NULL;
         return;
     }
     $depth = $this->reader->depth;
     switch ($this->reader->nodeType) {
         case XMLReader::ELEMENT:
             $parent = $this->stack[$depth];
             $prefix = $this->reader->prefix;
             /* @var $node DOMElement */
             if ($prefix) {
                 $uri = $parent->lookupNamespaceURI($prefix) ?: $this->nsUriSelfLookup($prefix);
                 if ($uri === NULL) {
                     trigger_error(sprintf('Unable to lookup NS URI for element prefix "%s"', $prefix));
                 }
                 /* @var $doc DOMDocument */
                 $doc = $parent->ownerDocument ?: $parent;
                 $node = $doc->createElementNS($uri, $this->reader->name);
                 $node = $parent->appendChild($node);
             } else {
                 $node = $parent->appendChild(new DOMElement($this->reader->name));
             }
             $this->stack[$depth + 1] = $node;
             if ($this->reader->moveToFirstAttribute()) {
                 $nsUris = array();
                 do {
                     if ($this->reader->prefix === 'xmlns') {
                         $nsUris[$this->reader->localName] = $this->reader->value;
                     }
                 } while ($this->reader->moveToNextAttribute());
                 $this->reader->moveToFirstAttribute();
                 do {
                     $prefix = $this->reader->prefix;
                     if ($prefix === 'xmlns') {
                         $node->setAttributeNS('http://www.w3.org/2000/xmlns/', $this->reader->name, $this->reader->value);
                     } elseif ($prefix) {
                         $uri = $parent->lookupNamespaceUri($prefix) ?: @$nsUris[$prefix];
                         if ($uri === NULL) {
                             trigger_error(sprintf('Unable to lookup NS URI for attribute prefix "%s"', $prefix));
                         }
                         $node->setAttributeNS($uri, $this->reader->name, $this->reader->value);
                     } else {
                         $node->appendChild(new DOMAttr($this->reader->name, $this->reader->value));
                     }
                 } while ($this->reader->moveToNextAttribute());
             }
             break;
         case XMLReader::END_ELEMENT:
             $node = NULL;
             break;
         case XMLReader::COMMENT:
             $node = $this->stack[$depth]->appendChild(new DOMComment($this->reader->value));
             break;
         case XMLReader::SIGNIFICANT_WHITESPACE:
         case XMLReader::TEXT:
         case XMLReader::WHITESPACE:
             $node = $this->stack[$depth]->appendChild(new DOMText($this->reader->value));
             break;
         case XMLReader::PI:
             $node = $this->stack[$depth]->appendChild(new DOMProcessingInstruction($this->reader->name, $this->reader->value));
             break;
         default:
             $node = NULL;
             $message = sprintf('Unhandeled XMLReader node type %s', XMLReaderNode::dump($this->reader, TRUE));
             trigger_error($message);
     }
     $this->depth = $depth;
     $this->node = $node;
 }