Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda