第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

到達特定標(biāo)簽名稱時,如何在 php 中打破“foreach”?

到達特定標(biāo)簽名稱時,如何在 php 中打破“foreach”?

PHP
眼眸繁星 2021-11-26 19:38:07
我正在嘗試使用 php 將 .xml 表文件導(dǎo)入 mysql,它工作正常,但我想升級我的代碼,以便我可以識別 xml 表的更多變體。所以基本上問題是,我得到了我試圖閱讀的 .xml 文件的這段代碼(只是一個例子,我的真實表更大):...<Table ss:StyleID="s62">   <Column ss:StyleID="s62"/>   <Column ss:StyleID="s62"/>   <Column ss:StyleID="s62"/>   <Row ss:AutoFitHeight="0">     <Cell ss:StyleID="s75"><Data ss:Type="String">Mercado</Data></Cell>     <Cell ss:StyleID="s75"><Data ss:Type="String">Segmento</Data></Cell>     <Cell ss:StyleID="s76"><Data ss:Type="String">Codigo do Projeto</Data></Cell>   </Row>   <Row ss:AutoFitHeight="0">     <Cell ss:StyleID="s90"><Data ss:Type="String">Minera??o</Data></Cell>     <Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>     <Cell ss:StyleID="s90"/>   </Row>   <Row ss:AutoFitHeight="0">     <Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>     <Cell ss:StyleID="s90"/>     <Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>   </Row>   <Row ss:AutoFitHeight="0">     <Cell ss:StyleID="s90"/>     <Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>     <Cell ss:StyleID="s90"><Data ss:Type="String">Large CapEx&gt;&gt;maior que 500MBRL</Data></Cell>   </Row></Table><Worksheet ss:Name="cod">  <Table ss:StyleID="s62">... ...  </Table>...好吧,我想要做的是使用 getElementByTagName 獲取行和數(shù)據(jù)元素,但我只想獲取第一個 Table 元素中的內(nèi)容,而不是第二個、第三個等等......似乎“foreach($rows as $row)”正在從xml文件中獲取所有行,但我只想要“Table”標(biāo)簽中的內(nèi)容。我怎樣才能做到這一點??PS:稍后我還有另一個問題要解決,里面有很多行沒有項目(數(shù)據(jù)標(biāo)簽),所以我無法得到那些,程序只是跳到下一個,但我認為解決方案只是得到 '單元格”標(biāo)簽而不是“數(shù)據(jù)”。
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經(jīng)驗 獲得超4個贊

如果是這種情況,您應(yīng)該查找命名空間定義,這看起來像一個 OpenXML 電子表格。我希望你能找到xmlns="urn:schemas-microsoft-com:office:spreadsheet"和xmlns::ss="urn:schemas-microsoft-com:office:spreadsheet"。


這實際上是同一個命名空間,但 XML 屬性沒有默認命名空間,因此它們需要一個前綴/別名。


有了它,您可以使用 Xpath 表達式從文檔中獲取特定數(shù)據(jù):


$document = new DOMDocument();

$document->loadXML($xml);

$xpath = new DOMXpath($document);

$xpath->registerNamespace('spreadsheet', 'urn:schemas-microsoft-com:office:spreadsheet');


$records = [];

$rows = $xpath->evaluate('((//spreadsheet:Table)[1]/spreadsheet:Row)[position() > 1]');

foreach ($rows as $row) {

    $records[] = [

      'Mercado' => $xpath->evaluate('string(spreadsheet:Cell[1])', $row),

      'Segmento' => $xpath->evaluate('string(spreadsheet:Cell[2])', $row),

      'CodigoDoProjeto' => $xpath->evaluate('string(spreadsheet:Cell[3])', $row)

    ];

}



var_dump($records);

輸出:


array(3) {

  [1]=>

  array(3) {

    ["Mercado"]=>

    string(11) "Minera??o"

    ["Segmento"]=>

    string(10) "Portuário"

    ["CodigoDoProjeto"]=>

    string(0) ""

  }

  [2]=>

  array(3) {

    ["Mercado"]=>

    string(10) "Portuário"

    ["Segmento"]=>

    string(0) ""

    ["CodigoDoProjeto"]=>

    string(10) "Greenfield"

  }

  [3]=>

  array(3) {

    ["Mercado"]=>

    string(0) ""

    ["Segmento"]=>

    string(10) "Greenfield"

    ["CodigoDoProjeto"]=>

    string(30) "Large CapEx>>maior que 500MBRL"

  }

}

//spreadsheet:Tablefetch any Table,(//spreadsheet:Table)[1]將其限制為第一個,(//spreadsheet:Table)[1]/spreadsheet:Row返回第一個的Row元素Table。


spreadsheet:Cell[1]返回第一個Cell并string(spreadsheet:Cell[1])返回它的文本內(nèi)容。如果它不匹配一個節(jié)點,它將返回一個空字符串。


查看完整回答
反對 回復(fù) 2021-11-26
?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

您可以通過執(zhí)行 $tablas[0] 來僅訪問表數(shù)組中的第一個表?,F(xiàn)在您甚至不需要 foreach 循環(huán)。


<?php


$tabelas = $arquivo->getElementsByTagName("Table");


$tablea = $tabelas[0];


$rows = $tablea->getElementsByTagName("Row");


$contRow = 1;


foreach ($rows as $row) {

    if ($contRow > 1) {

        $Mercado = $row->getElementsByTagName("Data")->item(0)->nodeValue;

        $Segmento = $row->getElementsByTagName("Data")->item(1)->nodeValue;

        $CodigoDoProjeto = $row->getElementsByTagName("Data")->item(2)->nodeValue;


    }

    $contRow++;

}

?>


查看完整回答
反對 回復(fù) 2021-11-26
  • 2 回答
  • 0 關(guān)注
  • 204 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號