4

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>

4 Answers 4

6

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

echo $xml->MESSAGES->MESSAGE->RECIPIENT_NUM."<br>";
Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 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.
1

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);

1 Comment

DOM isn't more complex, just use DOMXpath: $template = $xpath->evaluate('string(//TEMPLATE)').
0

When you load the xml file using the php simplexml_load_file function to a variable. The veritable becomes an object.

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

So, in your case, the $xml variable becomes a multi-level object where every elements of xml file are key of the object. Like: TEMPLATE, PIN, COUNTRY_CODE, MESSAGES etc.

To access the data of the element, need to call like this bellow code.

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

Once you don this code, you will get the value under the TEMPLATE element.

But, as $xml is a multi-level object, to access the element under the MESSAGES, under MESSAGE like RECIPIENT_NUM, you have to code like bellow.

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

Here, MESSAGES is the key of $xml object. MESSAGE is the key of MESSAGES. RECIPIENT_NUM is the key of MESSAGE.

Finally, you will get the value of key element RECIPIENT_NUM = xxxxx

You can also access the value using php foreach loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.