Chris G
Chris G

Reputation: 7050

How do I find the text within an XML element with specific attributes?

I'm looking to parse an export from wordpress using Regex to import it into a custom blog application. I've tried a number of ways to try to get to the data, but have been unsuccessful. I have:

<category domain="category" nicename="category-name"><![CDATA[Category Name]]></category>

I'm looking to find all of the text where: <![CDATA[Category Name]]> is. It also match the attribute with domain="category", but I do not care what the "nicename" is. This is important because other <category> elements have domain="post_tag" in them, which I do not want.

Upvotes: 0

Views: 217

Answers (1)

MrCode
MrCode

Reputation: 64526

Using SimpleXML:

Demo

$obj = simplexml_load_string($xml);
foreach($obj->category as $c)
{
   if($c->attributes()->domain == 'category')
   {
     echo (string)$c; // echo the content
   }
}

Upvotes: 1

Related Questions