0

Long time listener, first time caller.

Here is what I got going on...

Created a PHP webservice, no problem with sending simple data types like string, int... No problem returning string or int.

Now I want to return data in XML.

So I created a DomDocument and filled in all the data...

Then I return it out of the service.

On my client side when I go to load the object as an XML document I get an error about my root not being properly formated.

I look at my debug and all the "special" characters have been escaped.

I did a hack and replaced "<" with > and so on.

Then I can load the result.

This does not seem right, I should be able to return XML with it properly formatted.

So I am guessing it is in how I defined the xml object???

Can someone post a sample of PHP defining the return of an xml object?

I have looked for days and I cannot find a simple example of selecting data from MySql turning it to XML and then returning the XML to a client.

user1441213
  • 169
  • 1
  • 6

3 Answers3

1
I did a hack and replaced "<" with > and so on.

There's no hack about replacing .XML harmful characters, its the rule. Read here about such characters. Instead, you can use this code:

function replace_characters_for_xml(&$str)
{
   $str = str_replace("&","&amp;",$str);
   $str = str_replace(">","&gt;",$str);
   $str = str_replace("<","&lt;",$str);
   $str = str_replace("'","&apos;",$str);
   $str = str_replace('"',"&quot;",$str);
}
...
replace_characters_for_xml($string);
0

So I created a DomDocument and filled in all the data...

I suspect that whatever you did here was done wrong.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Using the DomDocument is fairly hands off in terms of the creating the root. I getting this everywhere in the xml result. I readsomething about this happening when returning xml inside of xml. Which why I am thinking it deals with how I am returing the xml object in the service. – user1441213 Jun 07 '12 at 10:53
0

I found a different code example for storing MySql data into XML. This did the trick as to what I was looking for as a result.

Using PHP DOM to create XML files from MySQL data

Community
  • 1
  • 1
user1441213
  • 169
  • 1
  • 6