开发者

Sending xml using php

开发者 https://www.devze.com 2022-12-14 14:47 出处:网络
I have a xml file on my server. I have the following two questions. How to send this xml file using php to the clients browser ?

I have a xml file on my server. I have the following two questions.

  1. How to send this xml file using php to the clients browser ?
  2. The client would be making an ajax get request to the php script that sends the xml as a response. On the server side i use php simplex开发者_如何学Goml functions to parse xml data. But, on the client end what would be the best way to parse the xml data ?

Thank You


You would use these two commands:

<?php
header('Content-type: text/xml');
readfile('/path/to/file.xml');

You didn't say anything about receiving XML on the server, so I'm not sure what you mean by parsing.

Regarding parsing XML in JavaScript... I defer to someone with more experience.


gahooa's code to send the XML file to the client should work just fine. In JavaScript, you can use the XML parser to read and manipulate it. However, this is not the approach I would recommend.

Instead, let the server convert your XML into JSON after using PHP's file_get_contents() to read the XML to a string. JavaScript can natively evaluate JSON.

You'll find that navigating the DOM of an XML is clumsy by comparison to working with native JSON.


set the content type in the HTTP response using the header() function as:

header('Content-Type: text/xml');

then you can write the xml file content.

You must call hearder() before any output is sent.

On client side you can parse the XML in JavaScript using DOM.

0

精彩评论

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