0

I try to use PHP for and I'm pretty new to it. I have an xml file stored in a variable called $xmlroot. Here you can see my code:

<?php
    $xmlroot = $_POST['xmldata'];
    //echo "Your data is: " . $xmlroot;

    $doc = new DOMDocument;
    $doc->loadXML($xmlroot);

    for($i = 0; $i < count($xmlroot); $i++){
        echo $doc->getElementsByTagName('postalcode')->item($i)->nodeValue;
    }
?>

But it just displays me the first XML tag. It should display all tags called "postalcode". My XML file looks like this:

<root>
    <code>
        <postalcode>12345</postalcode>
        <name>Test1</name>
    </code>
    <code>
        <postalcode>67890</postalcode>
        <name>Test2</name>
    </code>
    <code>
        <postalcode>13579</postalcode>
        <name>Test3</name>
    </code>
    <code>
        <postalcode>02468</postalcode>
        <name>Test1</name>
    </code>
</root>

It should display 4 elements but it displays just 1. Any suggestions?

Thanks!

2 Answers 2

6

That is because count($xmlroot) is equal to 1. If you're going to use a for loop, you need to use the length property to retrieve the number of elements.

It's easier with a foreach loop, though:

$xmlroot = file_get_contents('file.html');

$doc = new DOMDocument;
$doc->loadXML($xmlroot);

foreach ($doc->getElementsByTagName('postalcode') as $postalcode) {
    echo $postalcode->nodeValue . '<br/>';
}

Demo.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice! You did it. Thank you very much! First I tried with length property, which doesn't work but then I noticed your edit. Foreach works perfect :) Thanks!
1

Use simplexml class in php:

e.g.

php

<?php

$xml = simplexml_load_string("<root>
    <code>
        <postalcode>12345</postalcode>
        <name>Test1</name>
    </code>
    <code>
        <postalcode>67890</postalcode>
        <name>Test2</name>
    </code>
    <code>
        <postalcode>13579</postalcode>
        <name>Test3</name>
    </code>
    <code>
        <postalcode>02468</postalcode>
        <name>Test1</name>
    </code>
</root>");

echo "<pre>";
print_r($xml);
echo "</pre>";

?>

Outputs:

SimpleXMLElement Object
(
    [code] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [postalcode] => 12345
                    [name] => Test1
                )
            [1] => SimpleXMLElement Object
                (
                    [postalcode] => 67890
                    [name] => Test2
                )
            [2] => SimpleXMLElement Object
                (
                    [postalcode] => 13579
                    [name] => Test3
                )
            [3] => SimpleXMLElement Object
                (
                    [postalcode] => 02468
                    [name] => Test1
                )
        )
)

so, to iterate, what you will do is:

foreach ($xml->code as $node)
{
    // Do something with $node - e.g.
    $postalcode = $node->postalcode;

    var_dump($postalcode);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.