I have an XML file which I need to parse using PHP and send the parsed data to Java, what are some best practices to accomplish it ?
-
Are there very limited options to do this ?Rachel– Rachel2009年10月20日 21:20:48 +00:00Commented Oct 20, 2009 at 21:20
-
I have to parse incoming XML, which contain decision rules, using PHP and than pass this decision rules to ILOG JRules Engine by using JRules API which is in Java, hope this clarifies the issue.Rachel– Rachel2009年10月22日 19:32:43 +00:00Commented Oct 22, 2009 at 19:32
-
2Is your Java application on the same machine where the XML is located? Can you read the XML directly from the Java application?Adrian– Adrian2009年10月23日 13:46:56 +00:00Commented Oct 23, 2009 at 13:46
-
No. It is on different Machine. Reading XML directly from Java Application can be one of the solution.Rachel– Rachel2009年10月23日 15:07:17 +00:00Commented Oct 23, 2009 at 15:07
-
What is the receiving Java application? Is it your own code or a pre-compiled Jar? What format does it expect the data in?James Anderson– James Anderson2009年10月28日 08:09:46 +00:00Commented Oct 28, 2009 at 8:09
10 Answers 10
This is a case study for a web service like SOAP.
2 Comments
If data size is quite big and you need fast transfer, then you would like to consider protobuf (http://code.google.com/p/protobuf/). It is a comparable to SOAP but communicates entirely in binary.
Comments
I guess it might help to know a little more about the premise of what you are trying to do and what your limitations are.
Are you only coding the PHP side and sending the data to a Java application that's already created? Or are you doing both sides? What kind of protocols are available to you to interface with Java?
//-> After Clarification //
I don't have much knowledge of the ILOG Engine, however I took a look at the API to kind of get an idea of where I would start.
Obviously I don't know how everything is setup for you but it appears that ILOG has an API for handling XML already built into it?
http://ilog.cn/products/jrules/documentation/jrules67/api/html/index.html
Personally I would make the required modifications to the XML file and hand it off to the Java program to take care of using that API.
Sorry if I went the wrong direction with this!
So you want to:
parse the xml.
Extract some data and insert it into MySql.
Analise the incoming data and construct some JRules parameters.
Then invoke Jrules.
Right?
As far as I can work out JRules doesnt have a command line API -- if it did you could just kick of the app using a System(...) call.
My best advice would be to do everything in Java!
JRules is running in Websphere so you have a Servlet engine running for sure. Java comes complete with all the APIs you need to parse XML, update a database and invoke JRules. Why go to the pain of invoking a Java class from php?
Alternativly! I notice that JRules has a web service interface. Somewhere on the web is a php SOAP::Client interface (its optional but should be provided as default in most php 5 distros)so you could configure the WebService on JRules, get the xml schema and load it up into the php engine. Its then relativly easy to use phps SOAP::Client classes to invoke the Web Service. See http://th2.php.net/manual/en/book.soap.php
Still prefer the pure Java option though.
Comments
What do you mean by "parse"? What, precisely, is the PHP code doing?
You could use something like SOAP or XMLRPC or Thrift or Protobuf to get the two languages talking to one another, but it might be simpler just to have the PHP code load the XML and simply send it byte-for-byte to the Java program via HTTP or something. Java code can then do the parsing work, which might be as easy as handing the XML data to the library you're using.
1 Comment
You can also use PHP SimpleXML (PHP v5) to read the data from the XML file
Comments
There is a PHP / Java bridge implementation: http://php-java-bridge.sourceforge.net/pjb/ my previous company used it once in a project and as far as I know it worked, but I was not involved in that project so I cant tell you exactly about any problems.
If that is not an option, a Webservice either via SOAP or maybe a more "lightweight" exchange format would be your best bet I guess.
Comments
Why do you have to parse this file using PHP? Is porting this code over to Java out of the question? What does the PHP code return? How easily can the be read by Java (or is this part of the overall question?)
As I see it you have the following options: Rewrite the logic in Java, a web service (SOAP, REST what have thee), a PHP/Java bridge or a PHP scripting engine in Java.
4 Comments
I would suggest this alternative-
a) Parse the XML and convert it into JSON (Java script object notation) in PHP. (Manipulating JSON objects are very easy, just like handling maps/arrays in PHP)
b) Do whatever manipulations you want to the JSON data.
c) Call a Java server side component (Servlet/Struts action etc., assuming your Java as a web component) and post the JSON data to the component.
d) Just read the request parameter (JSON data) and use a JSON library (sourceforge has a good JSON library for java) to parse the data into a Map/List (depending on what kind of data you have).
2 Comments
The following scenarios apply:
Create a php rest service, e.g. page which will return the xml file when requested by a standard web request (e.g. try opening the page in the brwoser). The Java application will then connect to the php rest service (e.g. page) and get the xml output.
The Java application starts a TCP server to which the PHP application connects to using the TCP protocol and sends the XML file to the Java application.
The PHP application saves the xml file in a column in a database table, the Java application reads it from there.