• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to parse/compile XML into Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Regards
Tom
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used Xmlbeans from Apache:
http://xmlbeans.apache.org/

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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 http://asm.objectweb.org/ ) 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.
     
    Author and all-around good cowpoke
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Bill
     
    Tom Hansen
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Hey cool! They got a blimp! But I have a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!