Page 1 of 1

Convert XML node value into string

Posted: Mon Jul 12, 2010 5:50 am
by sathyaparthi
HI

I need to convert xml node value into string. I am using

$file = file_get_contents("test.xml");

But i am getting whole xml file into string. I want only particular node value into string..


Thanks in advance

Re: Convert XML node value into string

Posted: Mon Jul 12, 2010 8:21 am
by patel.manjeet
Hi,

to read some node text u need to read XML file using php.

simple method to read XML in php is SimpleXML. Some sample code to read XML ..hope may useful ..

test.xml file

Code: Select all

<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
 <desc>
   <other>otherinfo1</other>
   <data>datainfo1</data>
 </desc>
</document>


PHP Code

Code: Select all

$filename = 'test.xml';    // xml file path/ url 
$xml = simplexml_load_file ($filename);  
echo $xml->title;   // Forty What?
echo $xml->desc->other;  // otherinof1