davidannis Posted March 7, 2014 Share Posted March 7, 2014 I am trying to get a large (~60MB) XML file into a database and it is giving me fits. A sample record from the XML file looks like this: <entry> <ent_seq>1002090</ent_seq> <k_ele> <keb>お手盛り</keb> <ke_pri>news2</ke_pri> <ke_pri>nf33</ke_pri> </k_ele> <k_ele> <keb>御手盛り</keb> </k_ele> <r_ele> <reb>おてもり</reb> <re_pri>news2</re_pri> <re_pri>nf33</re_pri> </r_ele> <sense> <pos>&n;</pos> <gloss>making arbitrary decisions which benefit oneself</gloss> <gloss>self-approved plan</gloss> </sense> </entry> So, I can have 1 or more <k_ele> elements and within each I can have 1 or more <ke_pri> elements. I need to decide what to do with the record based on the the content of ke_pri elements. Same issue with <r_ele> elements. So, I read the XML with SimpleXML and then because I don't know if each r_ele and re_pri is an object over which I need to iterate or a variable I have ugly code that looks like this: if (is_object($r_ele)) { foreach ($r_ele as $reading_element) { $re_pri = $reading_element->re_pri; if (is_object($re_pri)) { foreach ($re_pri as $value) { switch ($value) { // decide what to do here } } } else { switch ($re_pri) { // decide what to do here } } } } else { $re_pri = $reading_element->re_pri; if (is_object($re_pri)) { foreach ($re_pri as $value) { switch ($value) { // decide what to do here } } } else { switch ($re_pri) { // decide what to do here } } } I know that there must be a more elegant way to do this and would love suggestions of how I can improve my code. Quote Link to comment Share on other sites More sharing options...
Solution davidannis Posted March 8, 2014 Author Solution Share Posted March 8, 2014 For anyone who needs this in the future I decided that if the element was not an object I'd create a one element array and then I could use foreach in all cases. Still not elegant but a little better. I'm marking solved and moving on. 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.