1

So trying to take an xml provided with a url and then format the data into a html table with php. Am I meant to apply some kind of formatting to the xml so that it can read it as a simplexmlelement?

$url = 'myxmlurl'
$xml = new SimpleXMLElement($url);

and then I get these nice errors:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7

Warning: simplexml_load_string():  in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7

Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7

Edit: file_get_contents fixed the error. Now I'm getting a new on when I try to loop.

$xml = new SimpleXMLElement(file_get_contents($url));
foreach ($xml->assets->asset as $assetElement) {
  $html="
<tr>
  <td class='text-left'><strong>"/*.$assetElement.*/."</strong></td>
  <td class='text-left'><strong>".(string)$assetElement->effectiveDate."</strong></td>
  <td class='text-left'><strong>".(string)$assetElement->buyPrice."</strong></td>
  <td class='text-left'><strong>".(string)$assetElement->sellPrice."</strong></td>
</tr>";

}

It doesn't like my foreach

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 26
3
  • have you checked wheater string is valid or not ? Commented Jun 3, 2017 at 3:51
  • I just used the following to check it and it came back true: $xml = XMLReader::open($url); // The validate parser option must be enabled for // this method to work properly $xml->setParserProperty(XMLReader::VALIDATE, true); var_dump($xml->isValid()); Commented Jun 3, 2017 at 4:19
  • is this the whole code ? Commented Jun 3, 2017 at 4:29

1 Answer 1

3

Try adding file_get_contents():

$url = 'https://perennialonline.com.au/asset/?req=asset&auth=pere123&do=read_all&fund_id=PVDCIT&start_date=01-01-2013&end_date=01-01-2050'
$xml = new SimpleXMLElement(file_get_contents($url));

If you want to pass directly the url instead, check the signature of the SimpleXMLElement class:

final public SimpleXMLElement::__construct (
    string $data
    [, int $options = 0
    [, bool $data_is_url = false
    [, string $ns = ""
    [, bool $is_prefix = false ]]]]
)

You must code this:

$xml = new SimpleXMLElement($url, null, true);
echo $xml->asXml();
Sign up to request clarification or add additional context in comments.

1 Comment

If I solved your problem, please accept my answer as right

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.