Skip to content Skip to sidebar Skip to footer

Parse XML To HTML With PHP SimpleXMLElement

I am having some issues with SimpleXMLElement that I was hoping to get some help. I was reading about SimpleXMLElement and I built a PHP page to parse this XML:

Solution 1:

You only have one Pages so you are only entering that foreach once. Try looping on the urls.

$xml = "<?xml version='1.0'?>
<AdXML>

<Response>
    <Campaign>
        <Overview>
            <Name>strip</Name>
                <Description>category</Description>
                <Status>L</Status>
        </Overview>
        <Pages>
            <Url>page01</Url>
            <Url>page02</Url>
            <Url>page03</Url>
        </Pages>
    </Campaign>
</Response>
</AdXML>";
$xmlparsed = new SimpleXMLElement($xml);
foreach ($xmlparsed->Response->Campaign->Pages->Url as $url) {
    echo $url, PHP_EOL;
}

Output:

page01
page02
page03

Solution 2:

You can also use XPath.

foreach( $xml->xpath( 'Response/Campaign/Pages/Url' ) as $url ) {
    echo $url;
}

Post a Comment for "Parse XML To HTML With PHP SimpleXMLElement"