开发者

printing of xml

开发者 https://www.devze.com 2023-03-10 01:00 出处:网络
I want to print the array into xml format but i am unable to print it. Here is the code if(!empty($records开发者_如何学GoArray)){

I want to print the array into xml format but i am unable to print it. Here is the code

if(!empty($records开发者_如何学GoArray)){
print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n>");
print("<joblist>\n");   
foreach ($recordsArray as $data){   

print("".$data['user_id']."".$data['$task_id']."\n");

}
print("</joblist>\n");

} 
}

even i am unable to see

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n>

when i am doing view source its showing

<?xml version="1.0" encoding="UTF-8" ?> 
<joblist> 
<job><id>123</id><taskid>14</taskid></job> 
</joblist> 

now what to be done to display on the web page


If you want it to be viewed in the browser, instead of what is most likely your server's default, "text/html", you need to serve the file as "text/xml" or "application/xml".

You might not be able to see the tags because they are treated as bogus HTML (though they are no doubt in the source code if you view source)--though admittedly you should see any text inside the elements.

In any case, you will want to create a header (before you do any printing, including printing whitespace) anyways, to ensure it is displayed and treated as XML by the user's browser, by adding:

header('Content-type:application/xml;charset=utf8');


I see at least two problems:

  1. I don't think print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n>"); will generate valid xml, shouldn´t it be:

    print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");

  2. I don´t think print("<job><id>$data[$user_id]</id><taskid>$data[$task_id]</taskid></job>\n"); is going to work, try something like:

    print("<job><id>".$data[$user_id]."</id><taskid>".$data[$task_id]."</taskid></job>\n");


What happens if you run print_r($recordsArray)? It is possible that it is empty.


When I'm debugging and need to write xml to a browser I usually put it in a <textarea>. You need to print the results into a container that will display html tokens like < and > literally, or go through your string and convert them all to entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号