我堅持這一點,無法在網上找到答案。我想使用DOM來加載XML。 我有下列方案的XML:DOM XML解析限制結果值
<type1>
<other>...</other>
<number>bla</number>
<other>...</other>
</type1>
<type1>
<other>...</other>
<number>bla</number>
<other>...</other>
</type1>
...
<type2>
<other>...</other>
<number>bla</number>
<other>...</other>
</type2>
<type2>
<other>...</other>
<number>bla</number>
<other>...</other>
</type2>
Type1和Type2的兩個數據發生多次。標籤號碼出現在兩種類型中。 當我使用
$searchNode = $xmlHandler->getElementsByTagName("number");
我得到這兩種類型的數字。我怎樣才能得到type1或type2的數字?
更新: 基於Kami和Ikku的建議,我已經爲DOM解決了它。下面的工作代碼:
<?php
$xmlHandler = new DOMDocument();
$xmlHandler->load("xmldocumentname.xml");
$xpath = new DOMXPath($xmlHandler);
$searchNodes = $xpath->query("/type1");
foreach($searchNodes as $searchNode) {
$xmlItem = $searchNode->getElementsByTagName("number");
$number = $xmlItem->item(0)->nodeValue;
$xmlItem = $searchNode->getElementsByTagName("other");
$other = $xmlItem->item(0)->nodeValue;
echo "NUMBER=" . $number . "<br>";
echo "OTHER=" . $other . "<br>";
}
?>
我正在尋找一個DOM解決方案,但我想這個simplexml解決方案背後的想法是可行的。我會試試看,謝謝。 – Vincent
@Vincent這也可以使用DOM - 我已經爲你創建了一個樣本。 – Kami
我剛剛在DOM中完成它。它效果很好。 Kami mucho謝謝。 – Vincent