I am trying to extract values from a review xml file with deep node structure. I need to extract data from the parent node “reviews->reviews” AND from the child node “rating” WHERE ‘questionGroup’ = “DEFAULT_OPINION”. I tried several code variants, but can’t find a solution to get both values after searching for quite a while.
This is a part of the xml file:
<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>
This is my current code:
<?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>'; } ?>
I also tried:
<?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>'; } } ?>
In the first code I get the results with name and city and the rating from the first node. In the second code I get the review I would like to display as ratingvalue, but not the parentnode data. How do I get this review data together with the results of the first code?
Advertisement
Answer
You have to loop through your review content and add condition for getting review for particular question group type
<?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>'; } } }