Scrutters Posted December 1, 2009 Share Posted December 1, 2009 Hi, I need something list all the nodes of an XML file in a similar way to how the print_r() function lists out an array. So it will list out all the fields in the correct order but so that I can actually do something with them. Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) [d] => Array ( [0] => x [1] => y [2] => Array ( [0] => Keeps going deeper... [1] => etc... ) ) ) I'm trying to create a select box with all the fields in so that I can map the XML file into my database. At the moment I am doing this: foreach($xml->children() as $child1){ foreach($child1->children() as $child2){ foreach($child2->children() as $child3){ // Obviously this could keep going for ages... } } } Really need something that can test to see if there are any children nodes under the current node selected and keep going till it stops then move on the the next parent node. Or something that can count all nodes within the feed and just list them one by one. I'm sure there must be a way to do this, I just can't work out the logic. Quote Link to comment Share on other sites More sharing options...
salathe Posted December 1, 2009 Share Posted December 1, 2009 Given the array in your print_r-like example, what would the select box look like? Quote Link to comment Share on other sites More sharing options...
dominicd Posted December 1, 2009 Share Posted December 1, 2009 The code he is using is copied from a website it is as follows but it does not call the child nodes. $file = "disc.xml"; $xml_parser = xml_parser_create(); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } $data = fread($fp, filesize($file)); fclose($fp); xml_parse_into_struct($xml_parser, $data, $vals, $index); xml_parser_free($xml_parser); $params = array(); $level = array(); foreach ($vals as $xml_elem) { if ($xml_elem['type'] == 'open') { if (array_key_exists('attributes',$xml_elem)) { list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']); } else { $level[$xml_elem['level']] = $xml_elem['tag']; } } if ($xml_elem['type'] == 'complete') { $start_level = 1; $php_stmt = '$params'; while($start_level < $xml_elem['level']) { $php_stmt .= '[$level['.$start_level.']]'; $start_level++; } $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];'; eval($php_stmt); } } echo "<pre>"; print_r ($params); echo "</pre>"; Quote Link to comment Share on other sites More sharing options...
Scrutters Posted December 1, 2009 Author Share Posted December 1, 2009 Ideally the select box would end up something like this... <select name="test"> <optgroup label="Parent"> <option value="etc1">etc1</option> <option value="etc2">etc2</option> <optgroup label="etc3"> <option value="abc1">abc1</option> <option value="abc3">abc2</option> </optgroup> </optgroup> </select> I have recieved a reply on another forum which may have answered my question, but open to other suggestions. The suggestion I have had is to try one of the user exmaples on the PHP SimpleXML->children() function page. Quote Link to comment Share on other sites More sharing options...
salathe Posted December 1, 2009 Share Posted December 1, 2009 If you could show the XML and the expected HTML, any solutions for you would be easier to arrive at. Quote Link to comment Share on other sites More sharing options...
Scrutters Posted December 1, 2009 Author Share Posted December 1, 2009 Now this is the problem, there isn't just one XML file. There will be potentially numerous XML files in different formats. This is why I need something to list out all available nodes within the feed. Sorry a can't be any more specific, but this is what I need my system to do. Quote Link to comment Share on other sites More sharing options...
salathe Posted December 1, 2009 Share Posted December 1, 2009 Having example input and output would help us to see what kind of translation you're looking for given specific structures of the XML. To clarify, there are different ways to translate the following XML: <example> <a>apple</a> <a>orange</a> <b> <c>cherry</c> <d>damson</d> </b> <e>elderberry</e> </example> Given that (would you need to even cater for structures like that?) what would the HTML be? Quote Link to comment Share on other sites More sharing options...
Scrutters Posted December 1, 2009 Author Share Posted December 1, 2009 Using that example, the HTML would need to look to the following: (Or as close as I can get it) <select name="testSelect"> <optgroup label="example"> <option>apple</option> <option>orange</option> <optgroup label="b"> <option>cherry</option> <option>damson</option> </optgroup> <option>elderberry</option> </optgroup> </select> Does that help at all? Quote Link to comment Share on other sites More sharing options...
salathe Posted December 1, 2009 Share Posted December 1, 2009 And the option values? Quote Link to comment Share on other sites More sharing options...
Scrutters Posted December 1, 2009 Author Share Posted December 1, 2009 Sorry I thought by not putting values it would be obvious: <option value="apple">apple</option> Quote Link to comment Share on other sites More sharing options...
salathe Posted December 2, 2009 Share Posted December 2, 2009 Apologies for not replying to this yesterday, have you progressed at all towards a solution or still need a hand with it? Quote Link to comment Share on other sites More sharing options...
Scrutters Posted December 2, 2009 Author Share Posted December 2, 2009 I've managed to get it working, but need to look into putting the XML feed into an array on page load as I reference it multiple times on a page it creating quite a load on my server. I think I have the solution for that as well though. Cheers for the help. Quote Link to comment 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.