使用php提取父节点的属性值以及子节点

时间:2017-10-02 02:26:26

标签: php xml xmlreader

我正在处理大量嵌套的大型xml文件,并且由于文件较大,决定使用XML阅读器。我打算提取的是属性值#34; PartyID"来自父节点(N8:实体)和" OrganisationName"的文本值和" CompanyID",然后导出到csv。

从我的xml文件中,此信息的路径是:      OrganisationName =      N8:EntityList / N8:实体/ N2:OrganisationName / N2:NameElement      CompanyID =      N8:EntityList / N8:实体/ N5:标识符/ N5:标识符/ N5:IdentifierElement

我打算有一个包含此列标题的表:OrganisationName CompanyID PartyID。使用我的代码,我可以提取OrganisationName和CompanyID,而PartyID的列是空白的。

我已经梳理了stackoverflow以了解问题所在,但我找不到解决方案。我很乐意帮忙。

以下是我的代码。

<?php error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
ini_set("max_execution_time", 0);

$reader = new XMLReader();
$reader->open("[MY XML FILE][1]");
$fo = fopen("companiesnzbn0.csv", "w" );
fputs($fo, "name, id, NZBN".PHP_EOL);
while ( $reader->read())    {
if ( $reader->name == 'N8:Entity' &&
        $reader->nodeType === XMLReader::ELEMENT )    {
            $name = null;
            $id = null;
            $attrsPartyID = null;
            $newNode = $reader->expand();

            $nameNode = $newNode->getElementsByTagName('OrganisationName');
            if ( $nameNode->length > 0 ){
                $name = $nameNode[0]->getElementsByTagName('NameElement')-
 >item(0)->nodeValue;
            }

            $nzbNode = $newNode-
 >getElementsByTagName('UltimateHoldingCompany');

            foreach ($reader as $element) {
                $attrsPartyID = $element->getAttribute('PartyID');
            }

            $idNode = $newNode->getElementsByTagName('IdentifierElement');
            if ( $idNode->length > 0 ){
                $id = $idNode[0]->nodeValue;
            }

            $newName = str_ireplace(","," ",$name);

            fputs($fo,  $newName.",".$id.",".$attrsPartyID.PHP_EOL);

        }
 }
 fclose($fo);

1 个答案:

答案 0 :(得分:0)

 <?php
 error_reporting ( E_ALL );
 ini_set ( 'display_errors', 1 );
 ini_set("max_execution_time", 0);

 $reader = new XMLReader();

 $reader->open("my.xml");
 $fo = fopen("my.csv", "w" );
 fputs($fo, "name, id, NZBN".PHP_EOL);
 while ( $reader->read())    {


 if ( $reader->name == 'N8:Entity' &&
        $reader->nodeType === XMLReader::ELEMENT && $reader->localName == 
 'Entity')    {
            $name = null;
            $id = null;
            $attrsPartyID = null;
            $newNode = $reader->expand();

            $nameNode = $newNode->getElementsByTagName('OrganisationName');
            if ( $nameNode->length > 0 ){
                $name = $nameNode[0]->getElementsByTagName('NameElement')-
  >item(0)->nodeValue;
            }


            $attrsPartyID = (string)$reader->getAttribute('PartyID');


            $idNode = $newNode->getElementsByTagName('IdentifierElement');
            if ( $idNode->length > 0 ){
                $id = $idNode[0]->nodeValue;
            }

            $newName = str_ireplace(","," ",$name);

            fputs($fo,  $newName.",".$id.",".$attrsPartyID.PHP_EOL);

        }
 }
 fclose($fo);