Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how read the data from XML file using php

Tags:

html

file

php

xml

I Have a XML file which have data's in it,i want to reading the XML file get values and display the values,i am trying to display RECIPIENT_NUM and TEMPLATE ,but now i able to display only the TEMPLATE but i can't able to display the RECIPIENT_NUM.below is my code can any one guide me to display the RECIPIENT_NUM,thanks

XML

<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
   <VERSION>2.0</VERSION>
   <INVOICE_NUM>33</INVOICE_NUM>
   <PIN>14567894</PIN>
   <MESSAGE_TYPE>INSTANT_SEND</MESSAGE_TYPE>
   <COUNTRY_CODE>xxxxxxx</COUNTRY_CODE>
   <TEMPLATE>Dear Mrs Braem, this is a message from xxxxxxx. Kindly call us regarding your cleaning appoitnment tomorrow at 9.30. Thanks and Regards</TEMPLATE>
   <DATABASEINFO>
      <DATABASE_NAME>xxxxxx</DATABASE_NAME>
      <CLINIC_ID>1</CLINIC_ID>
   </DATABASEINFO>
   <MESSAGES>
      <MESSAGE>
         <SEND_DATE>2013-12-15</SEND_DATE>
         <ENTITY_ID>0</ENTITY_ID>
         <RECIPIENT_NUM>xxxxxxx</RECIPIENT_NUM>
         <MESSAGE_PARAMS />
      </MESSAGE>
   </MESSAGES>
   <CSUM>ffd6c84a1a89a0f2ebc8b1dc8ea1f4fb</CSUM>
</DOCUMENT>

PHP

<html>
<body>

<?php
$xml=simplexml_load_file("/data/data/www/Message.xml");
print_r($xml);

echo $xml->TEMPLATE . "<br>";
echo $xml->RECIPIENT_NUM."<br>";
?>  

</body>
</html>
like image 346
arok Avatar asked Dec 26 '22 14:12

arok


2 Answers

You have to look at the structure of the XML, you need to do

echo $xml->MESSAGES->MESSAGE->RECIPIENT_NUM."<br>";
like image 56
dave Avatar answered Dec 29 '22 11:12

dave


This works for me,

$xml = simplexml_load_file("/data/data/www/Message.xml");
foreach($xml->children() as $key => $children) {
  print((string)$children->TEMPLATE); echo "<br>";
  print((string)$children->RECIPIENT_NUM); echo "<br>";
  // Remaining codes here.
}

The simplexml_load_file() returns xml nodes as object, each element in that object can be read using children() and can be retrived as string value using the above code.

like image 40
Akhila V Nair Avatar answered Dec 29 '22 12:12

Akhila V Nair