从帖子请求PHP - JAVA显示XML文件

时间:2017-04-28 18:56:52

标签: java php xml

我有一个非常糟糕的情况,我必须将XML文件从java客户端传递到PHP页面。 PHP页面将简单地从POST读取xml文档并使用echo显示它。 但是当我使用echo时,我没有从PHP页面上的XML获得任何输出。 这是我的JAVA客户端:

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();
            }
    }
}    

这是我的PHP页面:

<?php
$dataPOST = trim(file_get_contents('php://input'));
$xmlData = simplexml_load_string($dataPOST);
echo $xmlData;
?>

这是我的XML文件:

<?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>

我有PHP代码从POST here读取XML 我还想解析xml文件。 请帮助!!!

0 个答案:

没有答案