0

I'm trying to read an XML file using PHP. The XML file is a report that comes from somewhere else. Everything works fine except when the XML file ends up having multiple tags with the same name within the 'ReportItem' tags.

For example, when there is only 1 tag, the contents of that tag are correctly displayed. But when there is more than 1 tag, it displays 'ARRAY(0x996c840)'.

My XML looks like this:

<Scan>
  <Report>
    <ReportHost> 


    <ReportItem>

        ...

        <see_also>http://google.com</see_also>

        ...

    </ReportItem>
    <ReportItem>

        ...

        <see_also>http://google.com</see_also>
        <see_also>http://google.com</see_also>
        <see_also>http://google.com</see_also>
        <see_also>http://google.com</see_also>
        <see_also>http://google.com</see_also>

        ...

    </ReportItem>

    ...

    </ReportHost> 
  </Report>
</Scan>

My Code looks like this:

//Load the XML File
$xml = simplexml_load_file($path);

foreach ($xml->ReportHost as $reportHost)
{

    $x = 1;

    foreach ($reportHost->ReportItem as $reportItem)
    {

        ...

        $ARCHIVES['SEE_ALSO'][$x] = str_replace( "\\n", '<br />', $reportItem->see_also);

        ...

    }
}

I've tried many different things, but nothing seems to work. Even just trying to display it doesn't even work.

I've tried:

print_r($xml->xpath("//see_also"));

But that still only gives me the contents if there is only 1 tag, otherwise I get the same 'ARRAY(0x996c840)' result.

I also tried:

$z = 0;                     
while (isset($reportItem->see_also[$z]) AND ($reportItem->see_also[$z] != ""))
{                           
    echo "See Also: " . $reportItem->see_also[$z] . "\n";
    $z++;
}

But I always get the same thing.

I even tried using an 'xml_to_array' function from xmlutils.php which was a script I downloaded from:

http://pynej.blogspot.com/2010/02/convert-xml-to-array-and-array-to-xml.html

But it still always displays 'ARRAY(0x996c840)' instead of the contents.

I really need someones help to see what I'm doing wrong. Thanks in advance for your help!

2
  • try to use php.net/manual/de/class.simplexmliterator.php perhaps you get a other result Commented Feb 26, 2014 at 15:11
  • @Gizzmo Just tried what you suggested, still get the same results. [see_also] => Array ( [0] => ARRAY(0x996c840) ) Commented Feb 26, 2014 at 15:28

1 Answer 1

1

must be:

foreach ($xml->Report->ReportHost as $reportHost)
...

You forgot the <Report> node.

see it working: https://eval.in/106449

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

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.