Skip to content
Advertisement

Retrieve OuterXML From XML Child Node

I need to retrieve the OuterXML for each speak tag.

For example, I need to retrieve this data for the first speak tag in test.ssml:

   <speak xmlns="https://www.w3.org/2001/10/synthesis" version="1.0" xml:lang="en-US">
      <voice name="en-US-GuyNeural">
         <prosody rate="0.00%">Test 1</prosody>
      </voice>
   </speak>

index.php

set_time_limit(0);
require_once('src/Config.php');
$fileName = __DIR__.DIRECTORY_SEPARATOR.'test.ssml';

$fileContent = file_get_contents($fileName);
// $fileContent = preg_replace( "/r|n/", "", $fileContent );
$xml=simplexml_load_file($fileName);
$reader = new XMLReader();

foreach($xml->speak as $child)
  {
  echo $child->getName() . " ::: " . htmlspecialchars( $reader->readOuterXml ( $child )  ). "<br>";
  }

test.ssml

all tracks.mp3 bookmarks.dat Test 1 Test 2

Current Output in Browser

Current Output Image

Desired Output

Desired Output Image

Advertisement

Answer

You can get the XML directly using the SimpleXML function asXML() and don’t need (as far as I can tell) the XMLReader

$xml=simplexml_load_file($fileName);

foreach($xml->speak as $child)
{
    echo $child->asXML()."<br />";
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement