tqla Posted June 11, 2012 Share Posted June 11, 2012 I am working with an XML file that is formatted like this: <pictures> <pix pic="image1.png" alt="Tooltip Text 1" desc="Content 1" /> <pix pic="image2.png" alt="Tooltip Text 2" desc="Content 2" /> </pictures> I need to load the file and make it into an array so that I can get the "pic", "alt" and "desc" information so that I can echo them. I try this but it prints nothing: $xml = simplexml_load_file('file.xml'); print_r($xml); This prints nothing too: $data = file_get_contents('file.xml'); print_r($data); I figure it's because of the format of the xml file but I cannot change that. All help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/ Share on other sites More sharing options...
insidus Posted June 11, 2012 Share Posted June 11, 2012 The problem you're having is because the simplexml_load_file is treated almost like a class, instead of a variable. this page will help you http://www.w3schools.com/php/php_xml_simplexml.asp try doing print_r($xml->getName()); Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/#findComment-1353020 Share on other sites More sharing options...
requinix Posted June 11, 2012 Share Posted June 11, 2012 There's one thing you have to keep in mind with SimpleXML: everything is an object. If you need a string then you should (and sometimes must) cast to a string, like (string)$xml->pix[0]->pic Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/#findComment-1353021 Share on other sites More sharing options...
tqla Posted June 11, 2012 Author Share Posted June 11, 2012 Testing your suggestions Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/#findComment-1353028 Share on other sites More sharing options...
tqla Posted June 11, 2012 Author Share Posted June 11, 2012 It seems to be something about the way the xml is formatted. Your suggestions work great with this: <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> </book> </catalog> But I cannot get a result with this: <pictures> <pix pic="image1.png" alt="Tooltip Text 1" desc="Content 1" /> <pix pic="image2.png" alt="Tooltip Text 2" desc="Content 2" /> </pictures> More help appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/#findComment-1353030 Share on other sites More sharing options...
tqla Posted June 11, 2012 Author Share Posted June 11, 2012 Works after all (syntax error). Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/264016-php-xml-help/#findComment-1353041 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.