разбирать атрибут XML через PHP XMLReader - PullRequest
2 голосов
/ 22 апреля 2010

следующие примеры кода XML.

<m:ad xmlns:m="http://www.w3c.org/soap">
    <title><![CDATA[TITLE]]></title>
    <phone>123456789</phone>
    <attributeGroup>
       <attribute id="14" name="A1">40</attribute>
       <attribute id="15" name="A2">50</attribute>
   </attributeGroup>
</m:ad>

Я знаю только PHP XMLReader для получения значения

$reader = new XMLReader();        
if ($reader->name == "title" && $reader->nodeType ==XMLReader::ELEMENT) {
    echo $reader2->read(); // will get TITLE   
 }

Но как получить атрибут А1, А2. Я хотел бы получить 40 и 50 оба.

1 Ответ

5 голосов
/ 22 апреля 2010
$reader = new XMLReader();
$reader->xml('<m:ad xmlns:m="http://www.w3c.org/soap">
    <title><![CDATA[TITLE]]></title>
    <phone>123456789</phone>
    <attributeGroup>
       <attribute id="14" name="A1">40</attribute>
       <attribute id="15" name="A2">50</attribute>
   </attributeGroup>
</m:ad>');


while ( $reader->read() ) {
  if (  $reader->nodeType ==XMLReader::ELEMENT && $reader->name == "attribute" ) {
    printf("id=%s, name=%s\n", $reader->getAttribute('id'), $reader->getAttribute('name'));
  }
}

печать

id=14, name=A1
id=15, name=A2
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...