3

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 ?

asked Oct 20, 2009 at 19:18
6
  • Are there very limited options to do this ? Commented 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. Commented Oct 22, 2009 at 19:32
  • 2
    Is your Java application on the same machine where the XML is located? Can you read the XML directly from the Java application? Commented Oct 23, 2009 at 13:46
  • No. It is on different Machine. Reading XML directly from Java Application can be one of the solution. Commented 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? Commented Oct 28, 2009 at 8:09

10 Answers 10

4
+75

This is a case study for a web service like SOAP.

answered Oct 20, 2009 at 19:20
Sign up to request clarification or add additional context in comments.

2 Comments

"SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks." It's the specification... I thought everyone could draw the line with the first sentence in the link I gave.
Well he was right, it's not a web service. It's a protocol specification. It's intended to be used by web services, but it isn't a web service.
2

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.

answered Oct 20, 2009 at 20:53

Comments

2

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!

answered Oct 22, 2009 at 19:30

2 Comments

Thank you for your efforts BrandonCS.
Why not use the java api to parse the rules?
2

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.

answered Oct 28, 2009 at 9:35

Comments

1

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.

answered Oct 27, 2009 at 16:01

1 Comment

PHP code is taking an XML file and parsing it. By parsing I mean XML Parsing and than I have to pass this parsed data to Java.
0

You can also use PHP SimpleXML (PHP v5) to read the data from the XML file

answered Oct 23, 2009 at 13:45

Comments

0

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.

answered Oct 23, 2009 at 13:49

Comments

0

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.

answered Oct 23, 2009 at 13:52

4 Comments

You are right, How easily can be ready by Java is the overall question. I have to parse XML using PHP and than I need to parsed data to Java.
Why do you /have to/ parse the data in PHP? What does the data look like coming out of PHP process or has that not been decided yet? If not what else is the PHP process doing other than "parsing" the data? And what does parsing mean in this case?
It is an simple case of parsing XML file we can use perl or php, after parsing data needs to go into mysql database and decision tables need to go to JRules Engine. Data coming out of PHP process should be data and decision table as mentioned, php need to store data to mysql database and pass decision rules to JRules engine as mentioned. Parsing = What parsing means in "Parsing XML File"
I think you answered your own question... Parse the data by PHP and load into a mysql database and have the JRules engine access the data out of the the mysql database. I think you are asking a rather obtuse question that doesn't really need to be asked.
0

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).

answered Oct 28, 2009 at 7:21

2 Comments

JSON XML what difference does it make? Both php and Java handle xml very nicely why bother with a protocol conversion?
I feel JSON is a lighter representation of structured data and is closer and intuitive to the data structures we use generally use. I also think XML requires more effort to be parsed efficiently than JSON.
0

The following scenarios apply:

  1. 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.

  2. 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.

  3. The PHP application saves the xml file in a column in a database table, the Java application reads it from there.

answered Oct 29, 2009 at 8:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.