duanguane1670 2014-06-11 08:16
浏览 42

使用Java将XML文件作为DOM Document发布到php页面

I want to POST an XML file as DOM Document to a php page (received using Java).

When the Object is received in the php side. I read/traverse the file as a DOM Documents and send another XML DOcument file as response to the post.

Any directions would be very much appreciated.

Here's my sample Java code..

private Document sendToServerAndFetchResponse(Document xmlDocument) {

        Document responseXML = null;

        // Create the httpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();

        // Url to which the post has to be performed.
        HttpPost httppost = new HttpPost(
                "http://192.168.0.19:3334/cogglrestservice.svc/InsertTrack");

        // Make sure the server knows what kind of a response we will accept
        httppost.addHeader("Accept", "text/xml");

        // Also be sure to tell the server what kind of content we are sending
        httppost.addHeader("Content-Type", "application/xml");

        try {
            StringEntity entity = new StringEntity(xmlDocument.toString(),
                    "UTF-8");
            entity.setContentType("application/xml");
            httppost.setEntity(entity);

            // execute is a blocking call, it's best to call this code in a
            // thread separate from the ui's
            HttpResponse response = httpClient.execute(httppost);

            BasicResponseHandler responseHandler = new BasicResponseHandler();

            String strResponse = null;

            if (response != null) {
                try {

                    // Returns the response body as a String if the response was
                    // successful (a 2xx status code).
                    // If no response body exists, this returns null. If the
                    // response was unsuccessful (>= 300 status code), throws an
                    // HttpResponseException.

                    strResponse = responseHandler.handleResponse(response);
                } catch (HttpResponseException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {

                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return responseXML;
    }
}
  • 写回答

1条回答 默认 最新

  • douhuxi4145 2014-06-11 15:21
    关注

    DOM is available in PHP, too. Your data (the request body) should be available as stdin. Here is a simple demo:

    // create a dom and load the request body
    $dom = new DOMDocument();
    $dom->loadXml(file_get_contents('php://stdin'));
    
    // create an xpath instance for the document
    $xpath = new DOMXpath($dom);
    
    // look for nodes and set the attribute
    foreach ($xpath->evaluate('/foo/bar') as $node) {
      $node->setAttribute('attr', 'success');
    }
    
    // send the header and output the document as string
    header('Content-Type: application/xml');
    echo $dom->saveXml();
    

    Demo: https://eval.in/161089

    You should recognize the methods of DOMDocument from Javas xmlDocument.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看