Jump to content

XML parse invalid character error


singlephile

Recommended Posts

I've added a google map to my site and when a user registers it asks for their address which it then geocodes and adds to the map (eg finds the latitude and longitude). Now normally this is in the US and i don't have a problem, but now and again i'll have someone register from, say, germany and the xml returned by google contains foreign characters and the parsing crashes and gives and "Invalid character" error. Here's an example of the xml:

 

http://maps.google.com/maps/geo?q=Marktstr,M%F6nchengladbach,Germany&output=xml&key=ABQIAAAABF49M8jz0TUmr8jXDZwxsRS8-uVPCF0YzVBqSr0MDYVbXh-zfhT27vCkbr5-6YvIIf7aVUFJNXewFw

 

Any ideas out there?

Link to comment
https://forums.phpfreaks.com/topic/49889-xml-parse-invalid-character-error/
Share on other sites

Here's the code i use to parse:

 

      if (! ($xmlparser = xml_parser_create()) )

      {

      die ("Cannot create parser");

      }

      xml_set_element_handler($xmlparser, "start_tag", "end_tag");

      xml_set_character_data_handler($xmlparser, "tag_contents");

 

 

      $filename = 'http://maps.google.com/maps/geo?q=Marktstr,M%F6nchengladbach,Germany&output=xml&key=ABQIAAAABF49M8jz0TUmr8jXDZwxsRS8-uVPCF0YzVBqSr0MDYVbXh-zfhT27vCkbr5-6YvIIf7aVUFJNXewFw';

 

        if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); }

     

      while ($data = fread($fp, 4096)){

        $data=eregi_replace(">"."[[:space:]]+"."< ",">< ",$data);

        if (!xml_parse($xmlparser, $data, feof($fp))) {

            $reason = xml_error_string(xml_get_error_code($xmlparser));

            $reason .= xml_get_current_line_number($xmlparser);

            die($reason);

      }

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.