Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda