How to use XMLReader to parse multiple, identically named attributes of XML element/sub-element

馋奶兔 提交于 2019-12-06 15:06:31

Your first problem is that you are not checking for the appropriate nodeType, which is in fact related to the comment you linked: it matches both for the opening tag (ELEMENT) and the closing tag (END_ELEMENT).

Your second issue is also related to the missing nodeType check. After you fix that, you just have to check for the node's name to find out if it's a <role> or <person>.

Since I'm assuming you're also reading a large XML file, you probably want to know when you're passing to the next person tag... (via the END_ELEMENT nodeType) See my example below:

while($p->read()) {
    // check for nodeType here (opening tag only)
    if ($p->nodeType == XMLReader::ELEMENT) {
        if ($p->name == 'person') {
            if ($this->_notImported('govtrack',$p->getAttribute('id'))) {
                // $insert['indiv_*'] stuff here
            } else {
                $insert = null; // skip record because it's already imported
            }
        } else if ($p->name == 'role') {
            // role stuff here
            $startdate = $p->getAttribute('startdate');
        }

    // check for closing </person> tag here
    } else if ($p->nodeType == XMLReader::END_ELEMENT && $p->name == 'person') {
        if (isset($insert)) {
            // db insert here
        }
    }
}

By the way, your quotes must be replaced with proper quotes ' if you want this to work.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!