Search XML tags

Asked

Viewed 50 times

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 to do is scan this code, and create an array with the name of the list.

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

I tried to:

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

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

  • "similar to a Document XML". It is an xml or not, because in the title is "Search XML tags", so it would be easy to help.

  • It is not XML, it is just a string, in the format I indicated. If it were XML I could use several examples, but the format is the one quoted in the question:

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

1 answer

0

Tries to convert your string into 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);

Ai you can do a for to iterate over the items

Browser other questions tagged

You are not signed in. Login or sign up in order to post.