Created
September 15, 2016 18:25
-
-
Save ikenfin/0421321e4c414a80149901c6f1a4005d to your computer and use it in GitHub Desktop.
XMLReader wrapper test for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<root> | |
<row num="1"> | |
<item name="name">ИМЯ</item> | |
<item name="phone">Телефон</item> | |
<item name="email">ПОЧТА</item> | |
</row> | |
<row num="2"> | |
<item name="name">NAME 2</item> | |
<item name="phone">Телефон 2</item> | |
<item name="email">ПОЧТА 2</item> | |
</row> | |
<row num="3"> | |
<item name="name">ИМЯ 3</item> | |
<item name="phone">Телефон 3</item> | |
<item name="email">ПОЧТА 3</item> | |
</row> | |
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('TestReader.php'); | |
$reader = new TestReader(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test.xml'); | |
$reader->parse(); | |
var_dump($reader->getResult()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('AbstractAdvertisementXMLReader.php'); | |
class TestReader extends AbstractAdvertisementXMLReader { | |
private $num = 0; | |
public function parseRow() { | |
if($this->reader->nodeType == XMLREADER::ELEMENT && $this->reader->localName == 'row') { | |
$this->num = $this->reader->getAttribute('num'); | |
$this->result[$this->num] = array(); | |
} | |
} | |
public function parseItem() { | |
if($this->reader->nodeType == XMLREADER::ELEMENT && $this->reader->localName == 'item') { | |
$attr = $this->reader->getAttribute('name'); | |
// чтобы получить текст внутри тега, нужно заставить XMLREADER читать дальше | |
$this->reader->read(); | |
if($this->reader->nodeType == XMLREADER::TEXT) | |
$this->result[$this->num][$attr] = $this->reader->value; | |
} | |
} | |
} |
Нзч:)
а как быть если узел имеет пространство имен с двоеточием
<price:Minsk version="1612183484">
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Все понял, суть покопавшись в методах Вашего класса, public function parseField(), где Field() - имя элемента, еще раз спасибо за пример.