How to solve: XML Parsing Error: no element found

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

How to solve: XML Parsing Error: no element found

Post by plusnplus »

Hi all,
I try to get google weather info into my website.
when i try this code:

Code: Select all

<?php
header("content-type: text/xml");
$xmlData = file_get_contents("http://www.google.com/ig/api?weather=new%20york");
echo $xmlData;
?>
i got error : XML Parsing Error: no element found

but when i directly type http://www.google.com/ig/api?weather=new%20york into the browser, it give me the xml file.
or.. it that possible the error because, my company block something in firewall?

Any idea how to solve this problem?

Thanks for any reply/ help
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: How to solve: XML Parsing Error: no element found

Post by Zyxist »

This is a browser error, not PHP. What browser are you testing it? Do you have allow_url_fopen enabled to use HTTP in file_get_contents()?
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

Zyxist wrote:This is a browser error, not PHP. What browser are you testing it? Do you have allow_url_fopen enabled to use HTTP in file_get_contents()?
Hi Zyxist,
I user firefox 3.6
when i test with phpinfo(), allow_url_fopen is both on (local value and master value).

if this one is browser error, why when i directly type http://www.google.com/ig/api?weather=new%20york into the browser, it display me the xml file?

is that something todo with my company router/ firewall policy/ rule?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: How to solve: XML Parsing Error: no element found

Post by Zyxist »

This is a browser error, because the code you have provided has nothing to do with XML, so it is impossible for it to generate XML errors. Change the Content-type to "text/plain" and check, what the browser shows.
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

Zyxist wrote:This is a browser error, because the code you have provided has nothing to do with XML, so it is impossible for it to generate XML errors. Change the Content-type to "text/plain" and check, what the browser shows.
Hi Zyxist,
It show nothing, try on firefox 3.6 and ie 7
when i use text/xml, in both browser give same error message.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: How to solve: XML Parsing Error: no element found

Post by markusn00b »

The problem is that it isn't valid XML. You might try to prepend the XML declaration to the string.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: How to solve: XML Parsing Error: no element found

Post by phdatabase »

Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: How to solve: XML Parsing Error: no element found

Post by Zyxist »

If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

Code: Select all

var_dump(strlen($xmlData));
It will show you the length of the data received from Google.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: How to solve: XML Parsing Error: no element found

Post by phdatabase »

Zyxist wrote:If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

It will show you the length of the data received from Google.
NOT necessarily true. If you see nothing after running the script, check the page source.
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

Zyxist wrote:If it shows nothing, it means that your script sends nothing (because it retrieves nothing). And this is why the browser complains about missing XML elements. You can check it by doing for example

Code: Select all

var_dump(strlen($xmlData));
It will show you the length of the data received from Google.
Hi Zyxist,
With var_dump(strlen($xmlData));, the screen show "int(0)"

but again, why when i type direct in url browser: http://www.google.com/ig/api?weather=new%20york
it show something like:

Code: Select all

<xml_api_reply version="1">
−
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
−
<forecast_information>
<city data="New York, NY"/>
<postal_code data="new york"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-06-03"/>
<current_date_time data="2010-06-04 00:05:31 +0000"/>
<unit_system data="US"/>
</forecast_information> ...
in my test php page, it is really nothing beside this code:

Code: Select all

<?php
header("content-type: text/xml");
$xmlData = file_get_contents("http://www.google.com/ig/api?weather=new%20york");
echo $xmlData;
?>
or.. should i put something html tag(head, title, body, etc..)?

Thanks for everyone who try to help me.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to solve: XML Parsing Error: no element found

Post by Jonah Bron »

plusnplus wrote:
phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.
Did you check the page source?
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

Jonah Bron wrote:
plusnplus wrote:
phdatabase wrote:Try it without the header. You're just doing a client-server file transfer here, not creating an HTTP connection.
Hi phdatabase,
Without header, the screen show nothing/ empty.
Did you check the page source?

Hi Jonah Bron,
I did check, it is empty.

hm.., i really need weather info into my webpage.
Anyone can help :)
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: How to solve: XML Parsing Error: no element found

Post by Zyxist »

phdatabase -> but in this case it must be true. It's just about connecting three simple facts about what he sees and what Content-type he used.

plusnplus -> try this script locally. If the problem appears on server only, you must contact the administrator. Maybe outgoing HTTP connections are not allowed there. Anyway, does the problem still exists there, if you use cURL?
plusnplus
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 9:58 pm

Re: How to solve: XML Parsing Error: no element found

Post by plusnplus »

Zyxist wrote:phdatabase -> but in this case it must be true. It's just about connecting three simple facts about what he sees and what Content-type he used.

plusnplus -> try this script locally. If the problem appears on server only, you must contact the administrator. Maybe outgoing HTTP connections are not allowed there. Anyway, does the problem still exists there, if you use cURL?
Hi Zyxist,
1. What you mean by try it locally?
i test the scrip in my test server(win xp+apache).
when i try in my live server(ubuntu 8.04+apache), it also give the same error.

2. what you mean by http connection are not allowed?
like i meantion before i can get xml file if i type directly in browser:http://www.google.com/ig/api?weather=new%20york

3. how to use cURL?

yes, the problem still exist :(

anyway. thanks for your help/ reply
Post Reply