簡體   English   中英

使用DOMXpath進行XML解析,顯示所有子元素

[英]XML Parsing with DOMXpath showing all child elements

我有一個自動轉換類的這一部分,它會自動遍歷XML文件:

$xpath = new DOMXPath($doc);
    $entries = $xpath->evaluate($this->item,$doc);
    $csv = array();
    $i = 1;

    foreach($entries as $el) {
        foreach($el->childNodes as $node) {

            if(!empty($map)) {

                // not a real node OR not in the mapped array, and only mapped fields should be output
                if($node->nodeType==3 or (!in_array($node->nodeName,$map) and $this->selectedFields)) continue;

                if(!in_array($node->nodeName,$map)) {

                    $csv[0][$node->nodeName]=$node->nodeName;
                    $csv[1][$node->nodeName]=$node->nodeName;
                    $csv[$i][$node->nodeName] = $node->textContent;    

                } else {

                    $csv[0][$node->nodeName]=$remap[$node->nodeName];
                    $csv[$i][$remap[$node->nodeName]] = $node->textContent;    

                }
            } else {

                if($node->nodeType==3) continue;
                $csv[0][$node->nodeName]=$node->nodeName;
                $csv[1][$node->nodeName]=$node->nodeName;
                $csv[$i][$node->nodeName] = $node->textContent;    
            }
        }
        $i++;
    }

XML樹的一項如下所示:

<cac:Item>
    <cbc:Description>BREMSBELAG GALFER ORGAN. FD171-G1054 (KBA)</cbc:Description>
    <cac:SellersItemIdentification>
        <cac:ID>04303400</cac:ID>
    </cac:SellersItemIdentification>
    <cac:StandardItemIdentification>
        <cac:ID identificationSchemeID="EAN/UCC-13">8400160001718</cac:ID>
    </cac:StandardItemIdentification>
    <cac:ManufacturersItemIdentification>
        <cac:ID>FD171-G1054</cac:ID>
        <cac:IssuerParty>
           <cac:PartyName>
               <cbc:Name>Galfer</cbc:Name>
           </cac:PartyName>
        </cac:IssuerParty>
     </cac:ManufacturersItemIdentification>
     <cac:BasePrice>
         <cbc:PriceAmount amountCurrencyID="EUR">5.95</cbc:PriceAmount>
         <cbc:BaseQuantity quantityUnitCode="EA">1</cbc:BaseQuantity>
      </cac:BasePrice>
      <cac:RecommendedRetailPrice>
         <cbc:PriceAmount amountCurrencyID="EUR">10.9</cbc:PriceAmount>
         <cbc:BaseQuantity quantityUnitCode="EA">1</cbc:BaseQuantity>
      </cac:RecommendedRetailPrice>
</cac:Item>

輸出幾乎是完美的:

Beschreibung,Hartje-ID,EAN,“ Hersteller ID”,Einkaufspreis,Verkaufspreis,Packemenge,Artikelination,“ BREMSBELAG GALFER ORGAN。FD171-G1054(KBA)”,“ 04303400”,“ 8400160001718”,“ FD171-G10595 5. Ferfer”, 1“,” 10.9 1“

但是您可以看到RecommandedRetailPrice(Verkaufspreis)是從其子節點中混合而成的,但我需要它們各自的標頭單獨使用。

希望你有個主意。 謝謝。

自動轉換是一個非常糟糕的主意,您將失去DOM(和名稱空間)的結構信息,並且失去使用Xpath的可能性。 XML示例也已損壞。 缺少名稱空間定義(xmlns:cac =“”和xmlns:cbc =“”)。

DOM是數據源,您可以使用表達式(例如sql語句)來定義所需的數據源部分。 明確:

$dom = new DOMDocument();
$dom->load($xmlFile);
$xpath = new DOMXpath($dom);
// register namespaces - check the source document for the correct namespace strings
$xpath->registerNamespace('cac', 'urn:cac');
$xpath->registerNamespace('cbc', 'urn:cbc');

// define xpath expressions for the columns
$columns = [
  'Beschreibung' => 'string(cbc:Description)',
  'Hartje-ID' => 'string(cac:SellersItemIdentification/cac:ID)',
  'EAN' => 'string(cac:StandardItemIdentification/cac:ID)',
  'Hersteller ID' => 'string(cac:ManufacturersItemIdentification/cac:ID)',
  'Einkaufspreis' => 'number(cac:BasePrice/cbc:PriceAmount)',
  'Verkaufspreis' => 'number(cac:RecommendedRetailPrice/cbc:PriceAmount)',
  'Packemenge' => 'number(cac:BasePrice/cbc:BaseQuantity)'
];

// open a file stream for the standard output
$csvStream = fopen('php://stdout', 'w');

// write the columns names
fputcsv($csvStream , array_keys($columns));

//iterate all items
foreach ($xpath->evaluate('//cac:Item') as $item) {
  // a row for an item
  $row = [];
  foreach ($columns as $expression) {
    // use the expression to fetch data for the columns and add it
    $row[] = $xpath->evaluate($expression, $item);
  }
  // write to the output stream
  fputcsv($csvStream, $row);
}

輸出:

Beschreibung,Hartje-ID,EAN,"Hersteller ID",Einkaufspreis,Verkaufspreis,Packemenge
"BREMSBELAG GALFER ORGAN. FD171-G1054 (KBA)",04303400,8400160001718,FD171-G1054,5.95,10.9,1

暫無
暫無

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

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