r/PHPhelp • u/RestAside • Oct 23 '22
Solved Reading an XML file in a PHP file
Hi, I'm very new to this and I'm looking for some help on displaying my XML data in a php file.
echo "<p> Cost of Rental per day: " .$product->rentalCost. "</p>";
echo "<p> Lenght of Rental: " .$product->lenghtOfRental. "</p>";
echo "<p> Search Tags: " .$product->searchTags. "</p>";
echo "<p> Product Condition: " .$product->productCondition. "</p>";
echo "<p> Operation of product: " .$product->operation. "</p>"; Im using these statement to handle the child elements of my XML file which is working great, but I need to access a child of a child element(?) so I need to print everything inside the 'extras' element.
<products>
<product code="Simpsons">
<name></name>
<description></description>
<rentalCost></rentalCost>
<lenghtOfRental></lenghtOfRental>
<searchTags></searchTags>
<parentalRating></parentalRating>
<productCondition></productCondition>
<operation></operation>
<extras>
<nameManufacturer />
<additionalCost></additionalCost>
<lateFees></lateFees>
</extras>
<reviews></reviews>
<picFile></picFile>
<relatedURL></relatedURL>
</product>Any help is greatly appreciated.
7
Upvotes
1
u/ElectroFlux07 Oct 23 '22
Take a look at XPath. It should be a good starting point in parsing XML for PHP.
1
5
u/cgrd Oct 23 '22
It should be accessible with children() like:
You can then use a foreach loop to iterate the values.