-2

    <root>
    <code>
    <command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
    </code>
    </root>

Excepted Output:

**AAA BBB CCC DDD EEE** 

Output

**AAA BBB CCC EEE DDD**
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
Murali
  • 1

2 Answers2

0

Try something along these lines:

$string = " <root>
    <code>
    <command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
    </code>
    </root>
";                                                                       
$doc = new DOMDocument();
$doc->loadXML($string);
$xpath = new DOMXPath($doc);
$targets = array();

$results = $xpath->query('//command//text()');
foreach ($results as $result)
{   
    array_push($targets, trim($result->nodeValue) ." ");
}
foreach($targets as $target)
{
 echo $target;        
}

Output:

AAA BBB CCC DDD EEE 
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
0

Without the code it is difficult to say what your error is. So here is an example hot to do it with DOM+Xpath:

$xmlString = <<<'XML'
<root>
    <code>
        <command>AAA BBB CCC <cmd>DDD</cmd> EEE</command>
    </code>
</root>
XML;
                                                                     
$document = new DOMDocument();
$document->loadXML($xmlString);
$xpath = new DOMXPath($document);

var_dump(
    $xpath->evaluate('string(//command)')
);

Output:

string(19) "AAA BBB CCC DDD EEE"

//command fetches any command element node in the document. string(//command) casts the first found element into a string - it returns the text content of the node.

If you have multiple command elements you can iterate the them and read the $textContent property.

foreach ($xpath->evaluate('//command') as $command) {
    var_dump($command->textContent);
}
ThW
  • 19,120
  • 3
  • 22
  • 44