viviosoft Posted February 19, 2012 Share Posted February 19, 2012 Hello all, I've been working on parsing through some XML data that is formatted as x-schema. It's been a challenge to say the least. I've looked at XMLReader and the like to see if I can parse through the data but with no luck. I found an approach that works but having trouble iterating over the nodes inside the nodes. I'll try to explain: Here's what I have so far. The code produces the following result set: [4] => Array ( [FARM:NAME] => Array ( [FARM:NAME] => Kenyon Farm [FARM:FARM] => [FARM:TOTALAREA] => 999 [FARM:BUSINESSFIELDS] => [bUSINESSFIELD:FIELD] => [bUSINESSFIELD:LABEL] => K9 [bUSINESSFIELD:TOTALROTATIONS] => 1 [bUSINESSFIELD:TOTALAREA] => 998 [bUSINESSFIELD:FIELDOPTIONS] => [bUSINESSFIELD:FIELD_BOUNDARY] => [bUSINESSFIELD:DATAFILE] => FieldBoundary_2682 [bUSINESSFIELD:ACTIVITIES] => [CUST:FARMS] => ) ) Here's the code that produces that array: $tuData=file_get_contents("xml/tim.xml"); $simple = $tuData; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); xml_parser_free($p); $getValue=""; $counter=0; print"<pre>"; print_r($vals); foreach($vals as $data) { if(isset($data['tag'])) { if($data['tag'] == 'FARM:NAME') { $key = $data['tag']; $counter++; } if(trim($key)!== '' && trim($data['tag']) !== '') { $arData[$counter][$key][$data['tag']] = $data['value']; } } } print"<pre>"; print_r($arData); Now to my problem. You'll notice that the array has a key called [bUSINESSFIELD:LABEL] ... in the attached xml file there can be more than one field for each farm. My current solution only gets the first field in the xml file (as the current example shows above) and moves to the next farm and creates a new array of data. I would like to get the other fields for each farm and "stuff" them in the same array. Array's are not my strong suite and would be grateful to anyone that could help me with this problem. Thanks a bunch! 17601_.txt Quote Link to comment Share on other sites More sharing options...
codebyren Posted February 19, 2012 Share Posted February 19, 2012 If you are set on using xml_parser_create etc. then please ignore this post because I'm basically just suggesting an alternative that (to me) seems a bit easier. Otherwise, you could look into using simplexml or even DOMDocument. They might be be a little tricky to start with, particularly because of the namespaces in your sample XML document (and the object oriented approach), but once you catch on it's easy usually enough to keep your head above the water. Consider simplexml: <?php $xml = file_get_contents('xml/tim.xml'); $xml = simplexml_load_string($xml); // Note that we call children($namespace, true) often. // This is to indicate which namespace (e.g. 'farm' in <farm:name>) we're interested in. $farms = $xml->children('export', true) ->account_item->children('cust', true) ->account->children('cust', true) ->farms->children('farm', true); echo 'There are ', count($farms), ' farms. <br>'; foreach ($farms as $farm) { $name = $farm->children('farm', true)->name; echo 'Current farm in loop: ', $name, '<br>'; $fields = $farm->children('farm', true)->businessfields->children('businessfield', true)->field; if ($fields) { foreach ($fields as $field) { $label = $field->children('businessfield', true)->label; echo "Found a field with label $label in farm $name<br>"; } } } exit; ?> The nested objects basically follow the flow of the XML document. Without namespaces, the above might be as simple as something like: <?php $farms = $xml->account_item->account->farms; // And then loop through each farm and do whatever you want with it. ?> Quote Link to comment Share on other sites More sharing options...
viviosoft Posted February 19, 2012 Author Share Posted February 19, 2012 This is great! This approach is much better than mine. Thanks so much! 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.