簡體   English   中英

使用php提取父節點的屬性值以及子節點的相應文本值

[英]Extract attribute value of parent node using php along with corresponding text values of child nodes

我正在處理大量嵌套的大型xml文件,由於文件較大,因此決定使用XML閱讀器。 我打算提取的是來自父節點(N8:Entity)的屬性“ PartyID”的值以及“ OrganisationName”和“ CompanyID”的文本值,然后導出到csv。

從我的xml文件中,此信息的路徑為:OrganisationName = N8:EntityList / N8:Entity / N2:OrganisationName / N2:NameElement CompanyID = N8:EntityList / N8:Entity / N5:Identifiers / N5:Identifier / 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);
 <?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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM