Jump to content

Getting names of child nodes in XML


davidannis

Recommended Posts

I am trying to get the names of the children nodes in XML

 

Here is the code:

        foreach ($pos->children() as $child) {
            echo "I never get here";
            echo $child->getName() . "\n";
        }
        //}
        echo "but the node has children---<br>";
        print_r($pos);

which I modeled on the code from http://www.php.net/manual/en/simplexmlelement.getname.php

 

and here is a sample of the output:

SimpleXMLElement Object
(
    [n] => SimpleXMLElement Object
        (
        )

)
but the node has children---
SimpleXMLElement Object
(
    [int] => SimpleXMLElement Object
        (
        )

)
but the node has children---
SimpleXMLElement Object
(
    [n] => SimpleXMLElement Object
        (
        )

)

Why can't I go through the chldren with foreach?

Link to comment
https://forums.phpfreaks.com/topic/286812-getting-names-of-child-nodes-in-xml/
Share on other sites

The source XML is huge but here's a sample:

<entry>
        <ent_seq>1000320</ent_seq>
        <k_ele>
            <keb>彼処</keb>
            <ke_pri>ichi1</ke_pri>
        </k_ele>
        <k_ele>
            <keb>彼所</keb>
        </k_ele>
        <r_ele>
            <reb>あそこ</reb>
            <re_pri>ichi1</re_pri>
        </r_ele>
        <r_ele>
            <reb>あすこ</reb>
        </r_ele>
        <r_ele>
            <reb>かしこ</reb>
        </r_ele>
        <r_ele>
            <reb>あしこ</reb>
            <re_inf>&ok;</re_inf>
        </r_ele>
        <r_ele>
            <reb>あこ</reb>
            <re_inf>&ok;</re_inf>
        </r_ele>
        <sense>
            <pos>&pn;</pos>
            <pos>&adj-no;</pos>
            <xref>何処</xref>
            <xref>此処</xref>
            <xref>其処</xref>
            <misc>&uk;</misc>
            <gloss>there (place physically distant from both speaker and listener)</gloss>
            <gloss>over there</gloss>
            <gloss>that place</gloss>
            <gloss>yonder</gloss>
        </sense>
        <sense>
            <stagr>あそこ</stagr>
            <stagr>あすこ</stagr>
            <pos>&n;</pos>
            <misc>&col;</misc>
            <gloss>genitals</gloss>
        </sense>
        <sense>
            <xref>あれほど</xref>
            <gloss>that far (something psychologically distant from both speaker and listener)</gloss>
            <gloss>that much</gloss>
            <gloss>that point</gloss>
        </sense>
    </entry>
    <entry>
        <ent_seq>1000360</ent_seq>
        <r_ele>
            <reb>あっさり</reb>
            <re_pri>ichi1</re_pri>
        </r_ele>
        <info>
            <audit>
                <upd_date>2012-08-04</upd_date>
                <upd_detl>Entry created</upd_detl>
            </audit>
            <audit>
                <upd_date>2012-08-04</upd_date>
                <upd_detl>Entry amended</upd_detl>
            </audit>
        </info>
        <sense>
            <pos>&adv-to;</pos>
            <pos>&vs;</pos>
            <misc>&on-mim;</misc>
            <gloss>easily</gloss>
            <gloss>readily</gloss>
            <gloss>quickly</gloss>
        </sense>
        <sense>
            <misc>&on-mim;</misc>
            <gloss>lightly (flavored food, applied makeup)</gloss>
        </sense>
    </entry>

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.