r/PHPhelp • • 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

4 comments sorted by

5

u/cgrd Oct 23 '22

It should be accessible with children() like:

$product->extras->children();

You can then use a foreach loop to iterate the values.

2

u/RestAside Oct 24 '22

Thank you so much! Can't believe I didn't think of that...smh

1

u/ElectroFlux07 Oct 23 '22

Take a look at XPath. It should be a good starting point in parsing XML for PHP.