Jump to content

Parsing XML file - There must be a more elegant way


davidannis

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.