簡體   English   中英

如何通過屬性值 PhP 對 xml 子項進行排序

[英]How to sort xml child via attribute value PhP

我通過 PHP 有一個 XML 文件,我試圖根據屬性“word”在 div 中按升序列出特定的子“word”。

我放了 sort($word); 沒有成功

這是我的代碼:

<?php
if(isset($_POST['submitSave'])) {
// Disable errors due to empty xml files
error_reporting(E_ALL & ~E_WARNING);

$domDoc = new DOMDocument('1.0');
$domDoc->preserveWhiteSpace = false;
$domDoc->formatOutput = true;
$xpath = new DOMXpath($domDoc);

// load xml file
try {
    $domDoc->load('./data/expression.xml');
} catch (\Throwable $th) {
    //throw $th;
}

if($domDoc->getElementsByTagName('expression')->length>0){
    // If we already have expression tag defined
    $expression = $domDoc->getElementsByTagName('expression')[0];
}else{
    // If we don't have any expression tag, i.e. file is empty
    $expression = $domDoc->createElement('expression');
}



  // Create child node for product and set id(attribute), word(child), 
  meaning(child)
$vocabulario = $domDoc->createElement('vocabulario');
$vocabulario->setAttribute('word', $_POST['word']);
$word = $domDoc->createElement('word', $_POST['word']);
$meaning = $domDoc->createElement('meaning', $_POST['meaning']);




$domDoc->appendChild($expression);
$expression->appendChild($vocabulario);
$vocabulario->appendChild($word);
$vocabulario->appendChild($meaning);



// convert returned node list into an array
$vocabularios = iterator_to_array(
// fetch all vocabulario elements
$xpath->evaluate('/expression/vocabulario')  
);
// sort the array
usort(
$vocabularios,
static function($a, $b) use ($xpath) {
    // compare the word child element
    return strcasecmp(
        $xpath->evaluate('string(word)', $a),
        $xpath->evaluate('string(word)', $b)
       );
      }
    );

// iterate the sorted array
foreach ($vocabularios as $vocabulario) {
// append node to the expression document element
// that moves nodes from their current position
$domDoc->documentElement->appendChild($vocabulario);
}




file_put_contents('./data/expression.xml', $domDoc->saveXML());

}
?>

output

    <?xml version="1.0" encoding="UTF-8"?>
<expression>
  <vocabulario word="piece">
    <word>piece</word>
    <meaning>pedaço</meaning>
  </vocabulario>
  <vocabulario word="ball">
    <word>ball</word>
    <meaning>bola</meaning>
  </vocabulario>
</expression>

因此,當我在 php 頁面中解析 xml 時,應該在“piece”之前列出“ball”一詞,按升序排列。

SimpleXML 不擅長處理節點,因此使用 DOM 會容易得多。

$document = new DOMDocument();
$document->preserveWhiteSpace = FALSE;
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);

// convert returned node list into an array
$vocabularios = iterator_to_array(
    // fetch all vocabulario elements
    $xpath->evaluate('/expression/vocabulario')  
);
// sort the array
usort(
    $vocabularios,
    static function($a, $b) {
        // compare the word attribute
        return strcasecmp($a->getAttribute('word'), $b->getAttribute('word'));
    }
);

// iterate the sorted array
foreach ($vocabularios as $vocabulario) {
    // append node to the expression document element
    // that moves nodes from their current position
    $document->documentElement->appendChild($vocabulario);
}

$document->formatOutput = TRUE;
echo $document->saveXML();

要按子元素文本內容排序,您只需更改比較 function。

usort(
    $vocabularios,
    static function($a, $b) use ($xpath) {
        // compare the word child element
        return strcasecmp(
            $xpath->evaluate('string(word)', $a),
            $xpath->evaluate('string(word)', $b)
        );
    }
);

暫無
暫無

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

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