chris_2001 Posted May 15, 2008 Share Posted May 15, 2008 <?xml version="1.0" encoding="utf-8"?> <CueCards Version="1"> <Card Question="Question" Answer="Answer" History="YNNYN"/> </CueCards> I want to get PHP to read the value of: Card Question = "" Answer = "" and the number of Ys and the number of Ns in History="" How would I go about doing this? I havn't used xml with PHP very much and can't find any good guides for what I want to do. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/ Share on other sites More sharing options...
RichardRotterdam Posted May 15, 2008 Share Posted May 15, 2008 php 5 has a great buildin class called simpleXML try looking that up Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-541934 Share on other sites More sharing options...
zq29 Posted May 15, 2008 Share Posted May 15, 2008 Have you checked out the manual pages? There is plenty of information and examples on parsing XML over there. Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-541936 Share on other sites More sharing options...
chris_2001 Posted May 15, 2008 Author Share Posted May 15, 2008 I was looking at it earlier and not knowing much about XML I couldn't get it to work because of the nature of the XML file <?xml version="1.0" encoding="utf-8"?> <CueCards Version="1"> <Card Question="Question" Answer="Answer" History="YNNYN"/> </CueCards> Anyone care to show me how? Sorry for my probably ignorance in advance :-\ Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-541955 Share on other sites More sharing options...
chris_2001 Posted May 15, 2008 Author Share Posted May 15, 2008 Anyone ??? Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-541970 Share on other sites More sharing options...
Mr. Despair Posted May 15, 2008 Share Posted May 15, 2008 Something like this? <?php $file = 'yourfile.xml'; $xml = simplexml_load_file($file); echo "Card Question = " . $xml->Card['Question'] . "<br />"; echo "Card Answer = " . $xml->Card['Answer'] . "<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-541981 Share on other sites More sharing options...
chris_2001 Posted May 15, 2008 Author Share Posted May 15, 2008 Something like this? <?php $file = 'yourfile.xml'; $xml = simplexml_load_file($file); echo "Card Question = " . $xml->Card['Question'] . "<br />"; echo "Card Answer = " . $xml->Card['Answer'] . "<br />"; ?> Yeah exactly, but what would I do when there is more than 1 Card Question and Card Answer and how would i read the # of Y and N in History="" Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-542098 Share on other sites More sharing options...
chris_2001 Posted May 15, 2008 Author Share Posted May 15, 2008 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-542113 Share on other sites More sharing options...
chris_2001 Posted May 15, 2008 Author Share Posted May 15, 2008 It only reads the first Card If i have <?xml version="1.0" encoding="utf-8"?> <CueCards Version="1"> <Card Question="Question" Answer="Answer" History="YNNYN"/> <Card Question="Question2" Answer="Answer2" History="YNNYN"/> </CueCards> It doesnt work. Any help on getting it to read the 2nd one too? Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-542174 Share on other sites More sharing options...
Barand Posted May 15, 2008 Share Posted May 15, 2008 try <?php $str = '<?xml version="1.0" encoding="utf-8"?> <CueCards Version="1"> <Card Question="Question" Answer="Answer" History="YNNYN"/> <Card Question="Question2" Answer="Answer2" History="YNNYN"/> </CueCards>'; $xml = simplexml_load_string($str); foreach ($xml->Card as $c) { echo $c['Question'], '<br>'; echo $c['Answer'], '<br>'; echo $c['History'], '<br>'; $k = count_chars($c['History'], 1); foreach ($k as $ch => $val) { echo chr($ch), ' : ', $val, '<br>'; } echo '<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105764-getting-php-to-read-an-xml-file/#findComment-542215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.