Convert XML node value into string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sathyaparthi
Forum Newbie
Posts: 7
Joined: Fri Jul 09, 2010 12:06 am

Convert XML node value into string

Post 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
patel.manjeet
Forum Newbie
Posts: 4
Joined: Mon Jul 12, 2010 5:17 am

Re: Convert XML node value into string

Post 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
Post Reply