how read the data from XML file using php

how to get data from xml file in php
how to get attribute value in xml using php
how to store data in xml file using php
php read xml file into array
php read xml from url
php parse xml to array
simple xml example
php loop through xml child nodes

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>

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

echo $xml->MESSAGES->MESSAGE->RECIPIENT_NUM."<br>";

PHP SimpleXML Parser, The PHP simplexml_load_string() function is used to read XML data from a string. Error Handling Tip: Use the libxml functionality to retrieve all XML errors when loading Assume we have an XML file called "note.xml", that looks like this:. Read Specific XML Elements Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows.

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.

PHP simplexml_load_file() Function, Example. Output the data from each element in the XML file: <?php $xml=​simplexml_load_file("note.xml"); echo $xml->to . "<br>"; echo $xml->from . "<br>"; 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.

DOM is more complex, but can do everything, try to visit this DOM vs. SimpleXML Try this:

$xml = new DOMDocument();
$xml->load('/data/data/www/Message.xml');
$data = array (           
      'template'       => $xml->getElementsByTagName('TEMPLATE')->item(0)->nodeValue,
       'recipient_num' => $xml->getElementsByTagName('RECIPIENT_NUM')->item(0)->nodeValue          
        );         
 var_dump($data);

PHP XML Tutorial: Create, Parse, Read with Example, XML is used to structure, store and transport data from one system to another. XML Document Example; How to Read XML using PHP; How to Create an XML DOM XML is used to access and manipulate XML documents. So the above PHP output is the output that we get when we read the XML file, books.xml, with the simplexml_load_file() function. Looking at this output, you can see that PHP converts the XML document into an array. file and puts them in an array. The <pre></pre> tags makes the output very readable.

Basic SimpleXML usage - Manual, Example #1 Include file example.php with XML string. <?php $xmlstr = <<< So far, we have only covered the work of reading element names and their values. First, enabled the database connection using the PHP function mysqli()with the database server name, user name, password and database name. Second, Created the object using the DOMDocument()function and opened the XML file by using the function load()with the file name. Next, used the PHP function getElementsByTagName()to get the tag content.

simplexml_load_file - Manual, Convert the well-formed XML document in the given file to an object. Returns an object of class SimpleXMLElement with properties containing the data held within the XML Please read the section on Booleans for more information. Use​  An XML Parser is a program that translates XML an XML document into a DOM tree-structure like document. CDATA is used to ignore special characters when parsing XML documents. PHP uses the simplexml_load_file to read XML documents and return the results as a numeric array

Learn How to Read XML Files: Main Tips on Simplexml_load_file, The PHP XML example below shows the script that in our books.xml file and gets their attribute values:. In this tutorial we will try to read a Simple XML File using PHP script. XML used to create common information formats and data encryption. Some of XML's advantages are its self-documenting which allows for internationalization in human language that support parsing requirements. By using XML it improved the exponential and simplified capacity of PHP 5.

Comments
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
  • Thanks for your suggestion Şivā SankĂr, I have updated with a description.
  • DOM isn't more complex, just use DOMXpath: $template = $xpath->evaluate('string(//TEMPLATE)').