我正在尝试从具有深节点结构的审阅xml文件中提取值。我需要从父节点“ reviews-> reviews”和子节点“ rating”中提取数据,其中“ questionGroup” =“ DEFAULT_OPINION”。我尝试了几种代码变体,但搜索了很长时间后找不到找到两个值的解决方案。
这是xml文件的一部分:
<reviews>
<reviews>
<reviewId>f46f317c-5009-4ec4-ac8d-2b173db2dde6</reviewId>
<reviewAuthor>K.</reviewAuthor>
<city>Haarlem </city>
<rating>10</rating>
<reviewContent>
<reviewContent>
<questionGroup>DEFAULT_OVERALL</questionGroup>
<questionType>INT</questionType>
<rating>10</rating>
<order>0</order>
<questionTranslation>Totaal score</questionTranslation>
</reviewContent>
<reviewContent>
<questionGroup>DEFAULT_ONELINER</questionGroup>
<questionType>TEXT</questionType>
<rating>Perfecte service en mooie kwaliteit bord. </rating>
<order>0</order>
<questionTranslation>Beschrijf uw ervaring in een regel</questionTranslation>
</reviewContent>
<reviewContent>
<questionGroup>DEFAULT_OPINION</questionGroup>
<questionType>TEXT</questionType>
<rating>
De service is perfect. Tijdens het bestelproces werden mijn vragen beantwoord middels chat berichtjes via de website. Het bord werd netjes verpakt geleverd en is van mooie kwaliteit. Erg blij mee.
</rating>
<order>0</order>
<questionTranslation>Beschrijf uw ervaring</questionTranslation>
</reviewContent>
<reviewContent>
<questionGroup>DEFAULT_RECOMMEND</questionGroup>
<questionType>BOOLEAN</questionType>
<rating>true</rating>
<order>0</order>
<questionTranslation>Aanbevelen?</questionTranslation>
</reviewContent>
</reviews>
<reviews>
这是我当前的代码:
<?php
$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=bhokkolsgogtaf9");
$reviewContents = $xml->xpath('//reviewContent[questionGroup="DEFAULT_OPINION"]/rating');
foreach ($xml->reviews->reviews as $review){
echo '<div class="kiyoh-shop-snippets" style="{$show_rating|escape:"htmlall":"UTF-8"}"><div class="rating-box"><div class="rating" style="width:' . ($review->rating*10) . '%"></div></div></div>';
echo 'Naam: '.$review->reviewAuthor.'<br>';
echo 'Woonplaats: '.$review->city.'<br>';
echo 'Review: '.$review->reviewContent->reviewContent->rating.'<br>';
}
?>
我也尝试过:
<?php
$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=bhokkolsgogtaf9");
foreach($xml->xpath('//reviewContent') as $item) {
$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//questionGroup[. ="DEFAULT_OPINION"]');
if($v[0]){
print $item->reviewAuthor.'<br>';
print $item->city.'<br>';
print $item->rating.'<br>';
}
}
?>
在第一个代码中,我从第一个节点获得名称和城市以及等级的结果。在第二个代码中,我得到要显示为等级值的评论,而不是父节点数据。如何获得此评论数据以及第一个代码的结果?
答案 0 :(得分:0)
您必须遍历您的评论内容,并添加条件以获取特定问题组类型的评论
<?php
$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=bhokkolsgogtaf9");
foreach ($xml->reviews->reviews as $review) {
echo '<div class="kiyoh-shop-snippets" style="{$show_rating|escape:"htmlall":"UTF-8"}"><div class="rating-box"><div class="rating" style="width:' . ($review->rating * 10) . '%"></div></div></div>';
echo 'Naam: ' . $review->reviewAuthor . '<br>';
echo 'Woonplaats: ' . $review->city . '<br>';
foreach ($review->reviewContent->reviewContent as $content) {
if ($content->questionGroup == 'DEFAULT_OPINION') {
echo 'Review: ' . $content->rating . '<br>';
}
}
}