Jump to content

Recommended Posts

Hi,

 

Hopefully someone can help me sort out what I'm trying to do.

 

Basically, I have a PHP page with a $_GET variable, which takes the value of a topic name ($_GET['topic']) from a previous page.

This is stored in a variable called $topic.

 

I am basically trying to use that value stored in a piece of PHP which retrieves data from an xml file.

I know what I want to do; it just isn't working, and maybe I am not writing it correctly, or there may be a better way of doing it.

 

The next piece of PHP after setting the $topic is :

 

$items = simplexml_load_file("'$topic'.xml");

$items = new SimpleXMLElement("'$topic'.xml", null, true);

 

Hopefully, what I'm trying to do is clearer now!

 

So I want to use the topic name sent from a previous page in a link to parse a xml file named with the value in $topic.

 

Hope I've made myself clear.

 

The error I am getting from using the above script is :

 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "'neurology-neuroscience'.xml"

 

As you can see, the variable $topic is being printed into the statement fine, but it still returns the error.

 

NOTE: the file named neurology-neuroscience.xml does exist locally in the same directory, so the error isn't due to the file not being present.

 

Any help on this would be greatly appreciated  ;)

Link to comment
https://forums.phpfreaks.com/topic/139847-phpxml-troubles/
Share on other sites

Hello,

 

If you look at the error you will see the file name is 'neurology-neuroscience'.xml, notice the single quotes around the topic name?

 

In your php do this:

$items = simplexml_load_file($topic.".xml");
$items = new SimpleXMLElement($topic.".xml", null, true); 

 

See if that does the trick :)

Link to comment
https://forums.phpfreaks.com/topic/139847-phpxml-troubles/#findComment-731618
Share on other sites

If that don't work for ya, then try this... I use this method to get the xml file. I looked and it does not appear that there is any real performance difference between file_get_contents and simplexml_load_file. I actually had troubles getting simplexml_load_file to work properly. But it was probably just my own stupidity. :)

 

<?php
$file = $topic.'.xml';	
$xml = file_get_contents($file); 
$str = new SimpleXMLElement($xml);
print_r($str);
?>

 

Are you checking that the particular file does indeed exist before you try and load it? Since your taking data from $_GET, you should make sure that this file really does exist and handle it accordingly so you don't expose an error message that gives information you don't want your users to see.

 

nate

Link to comment
https://forums.phpfreaks.com/topic/139847-phpxml-troubles/#findComment-731626
Share on other sites

Hi, Thanks for both your replies. I removed the single quotes around the $topic varibale, and it now work  ;)

 

..BUT.....

 

With solving another problem, I always seem to create another!

 

On the first PHP page, there are 2 links.....

 

One is <a href="index.php?pageid=browsearticles&&topic=smoking">Smoking</a>

 

The other is <a href="index.php?pageid=browsearticles&&topic=neurology-neuroscience">Neurology-Neuroscience</a>

 

There are the 2 links that send the variable $topic to the next page.

 

For some reason, clicking the Smoking link works fine and returns data specific to the smoking.xml file fine.

But, when the Neurology link is clicked, this strange error is returned :

Warning: simplexml_load_file() [function.simplexml-load-file]: neurology-neuroscience.xml:6: I/O warning : failed to load external entity "DTD/xhtml1-strict.dtd"

 

Whats the deal with the DTD being printed as the $topic variable, when it was clearly sent using $_GET from the previous page? Its strange how the error only occurs with the neurology.xml file, and not the smoking.xml file.

 

Maybe a problem with the xml file?

 

Many thanks for your continued help!  ;)

Link to comment
https://forums.phpfreaks.com/topic/139847-phpxml-troubles/#findComment-731672
Share on other sites

Just found out one of the files (the neurology one) had a load of random html scrambled around in it - strange!

 

Anyway, cleaned it up and it works fine now, so many thanks for all your help!

 

Hopefully I don;t have any other problems!

 

Its always the smallest errors that create the biggest holdups when writing PHP I find!  :D

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/139847-phpxml-troubles/#findComment-731696
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.