I have a wiered situation where I have to pass XML file from java client to PHP page. PHP page will simply read the xml document from POST and display it using echo. But I am not getting any output from my XML on PHP page when I use echo. Here is my JAVA CLIENT:
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws Exception {
try {
URL url = new URL("http://localhost/xml.php");
String document = System.getProperty("user.dir")+"\\xml\\cars.xml";
FileReader fr = new FileReader(document);
char[] buffer = new char[1024*10];
int bytes_read = 0;
if ((bytes_read = fr.read(buffer)) != -1)
{
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("Content-Type","text/xml");
urlc.setDoOutput(true);
urlc.setDoInput(true);
PrintWriter pw = new PrintWriter(urlc.getOutputStream());
// send xml to php
pw.write(buffer, 0, bytes_read);
pw.close();
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Here is my PHP page:
<?php
$dataPOST = trim(file_get_contents('php://input'));
$xmlData = simplexml_load_string($dataPOST);
echo $xmlData;
?>
Here is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<supercars company="Ferrari">
<carname type="formula one">Ferrari 101</carname>
<carname type="sports">Ferrari 202</carname></supercars>
</cars>
I got code of PHP to read XML from POST here I would also like to parse xml file. Please Help!!!