Search XML tags [duplicate]

1

I have a string, similar to an XML document:

<lists>
   <list name='NOVOS' values='1'>
   <list name='OLDS' values='2'>
</lists>

What I need is to scan this code, and create an array with the name of the list.

Getting, for example, his return: 0 - > NEW and 1 - > OLDS, and so on.

I tried:

preg_match( '/name="([^"]*)"/i', $s, $lists_return );

Sometimes it works and sometimes only the first item in the list returns. The variable s is where the string I mentioned above is. It can be in regular expression.

    
asked by anonymous 03.11.2016 / 17:10

1 answer

0

Try to convert your string to an xml.

$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false; //elimina espaços em branco
$dom->formatOutput = false;
// carrega o xml 
$dom->loadXML($suaString, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG);

$dom->getElementsByTagName("tagQueVcQuerPegar")->item(0);

You can do a for iterate over the items

    
03.11.2016 / 18:00