Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Display XML file from post request PHP - JAVA

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

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!