My xml tag format is
<transcript>
<messages>
<message>
<to>user1@localhost.local</to>
<from>user2@localhost.local</from>
<body>hello</body>
<date>2014-09-09 14:14:17.652 IST</date>
</message>
<message>
<to>user2@localhost.local</to>
<from>user1@localhost.local</from>
<body>hi dear</body>
<date>2014-09-10 14:14:17.652 IST</date>
</message>
</messages>
</transcript>
I want to load This xml file and display result in format like
2014-09-09 - Tuesday
(2:14 PM ) user1 : hello
2014-09-10 - Wednesday
(2:14 PM )user2 : hi dear
I have tried this PHP code
$xml_file_path='filename_with_path';
$XMLReader->open($xml_file_path);
while ($XMLReader->read() && $XMLReader->name !== "transcript");
while ($XMLReader->read() && $XMLReader->name !== "messages");
while ($XMLReader->read() && $XMLReader->name !== "message");
while ($XMLReader->name === "message") {
$node = new SimpleXMLElement($XMLReader->readOuterXML());
}
but $node give me empty result,How can I fix it or where I am wrong.
I personally hate xml, I know it's a mature standard, but I hate working with non-native data stores. Even databases irk me.
If I may refactor your code a bit...
//This turns your xml into a php object, which can be manipulated more directly
if (file_exists('text.xml')) {
$xml = simplexml_load_file('text.xml');
}
then you can output pretty simply
$totalMessages = count( $xml->messages->message );
for($i = 0; $i < $totalMessages; $i++){
echo "{$xml->messages->message[$i]->date}<br>
{$xml->messages->message[$i]->from}: {$xml->messages->message[$i]->body}<br><br>";
}
This outputs
2014-09-09 14:14:17.652 IST
user2@localhost.local: hello
2014-09-10 14:14:17.652 IST
user1@localhost.local: hi dear
I leave it up to you to slice and dice your strings for prettiness.