File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to parse/compile XML into Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to parse/compile XML into Java" Watch "How to parse/compile XML into Java" New topic

How to parse/compile XML into Java

Tom Hansen

Joined: Jul 06, 2006
Posts: 4
Hi Experts,

I have a XML docment that contains a process description.

What I want to do is to create a process engine. The XML document should be used as input to this engine.

What I am looking for is a way to take care of the structure of the XML document and transfer it into a Java structure.

There is three steps to perform:
1. Send the XML document into the engine as input
2. Parse/compile the XML into Java objects and initiates the process.
3. The client calls the process and start it.

So I'm looking for a way to parse/compile XML into a equivalent Java structure. Specially I have a problem with how I can take care of the sequence in the process and transfer it into a Java structure, the process steps, so the client can call the process and get the process executed in the right way with the help of the Java structure.
Like this:

I know of the DOM, SAX and more parsers, so it's a bit further than that I'm thinking about.

Is there any known ways to do this?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

It sounds as if you need a XML-to-Java binding tool, of which there are many around. E.g., have a look at Jakarta Commons Digester.
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I have used Xmlbeans from Apache:

As long as you pay careful attention when settting the path names, it is easy to set up. The documentation is good also.
Tom Hansen

Joined: Jul 06, 2006
Posts: 4
Thanks for your suggestions,

the parsing or marshalling stuff is alright.
I don't know, it's a bit complicated to explain, the problem is that I first have to use the XML document as input in order to create the necessary classes/objects/interfaces. But inside the same XML document it is also some processing information that should be kept until a client ask for the process. This is the sequence I'm referring to. The purpose of the sequence is to perform the steps in the process in right order. But inside the sequence there is also elements that need to be initated into Java object first.
To sum it up: first I have to process the hole XML document in order to initiate all Java objects (say I have used XMLBeans or JAXB or something similar), inside the same document there is process information that should not be performed before a client ask for it, how can I transform the steps in the XML document into a Java data structure?

I might be on the wrong track here, so any suggestions how to make this work is welcome.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Would it be feasible to design Java structures that hold the processing information? They're just text in the XML document, why couldn't they be Java classes and data structures instead?
Tom Hansen

Joined: Jul 06, 2006
Posts: 4
Exactly what I want to do, I just can't see how to attack the problem.
This is a data structure that have to be dynamic I think(?)
Take this as a example:

As you see the assign element have a copy element that copy the value of flightdata in travelrequest to flightdata in flightdetails.
This is something that must be done runtime, i.e I can't do this when I initiate the XML document but when the client runs the process.
So how can I create a data structure in java
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
You have two options

  • Use XSLT transformation and custom style sheets to consume your XML file at build time and churn out Java classes.
  • Use some kind of byte code injection( like ASM ) and custom Classloaders to create Java classes on the fly.
  • Have a hybrid option - generate the entities that are static at build time using XSLT and the dynamic behavior at runtime that uses bytecode generation.

  • How much of runtime dynamics do you want to support? The build time option above is certainly simpler than the byte code injection option however, if your XML file changes then you need to rebuild the app.

    Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
    William Brogden
    Author and all-around good cowpoke

    Joined: Mar 22, 2000
    Posts: 13036
    This problem sounds sort of like the kind of things ANT does - take instructions and data from an XML file and string various operations together. ANT provides for writing custom tasks. If this was my problem I would seriously investigate ANT as a framework or at least as a model to study.

    Please keep us up to date with how you tackle this problem, it sounds interesting.

    Tom Hansen

    Joined: Jul 06, 2006
    Posts: 4
    Thanks again all,

    I will come back to you with my solution. After some investigation it seems like it can be a solution where I use a combination of XSLT and Ant.
    I agree. Here's the link:
    subject: How to parse/compile XML into Java
    It's not a secret anymore!