wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XML import advice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML import advice?" Watch "XML import advice?" New topic
Author

XML import advice?

Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Hi there... I could use some advice.
I'm developing an application that needs to import XML data objects from 3 different web-based data sources (it creates the appropriate URL and that URL returns XML, because the sites do not yet offer SOAP web services). The data objects hold pretty much the same information, but each of the 3 different data sources represents the data in their own XML format. The formats conform to DTDs.
My program has its own internal JavaBean that represents the data structures I am importing, so that I can manipulate the data.
What's the best way to convert the XML files from the 3 different sources into my program's in-memory data structure? I tried using Castor, thinking that I could convert the DTDs to XSDs, convert the data with Castor's generated files, and write my own conversion routines to change the data into my own Bean, but Castor choked on converting my DTD to an XSD.
Thanks for any advice!
Elliotte Rusty Harold
author
Ranch Hand

Joined: Feb 25, 2004
Posts: 91
You need to map the information in the documents you recieve into data structures appropriate for your local needs. These data structures need not be XML. They can be objects, lists, hash tables, database tables, or whatever makes sense for your problem.
Don't expect the mapping to be simple or easy or automatic. The general rule is that you have to write the code that will transform the data you're being fed into your internal data structures. There are no magic bullets for this problem. Mostly you just have to write the code.
Don't let yourself get trapped into thinking you have to model everything in the documents you receive, however. It's often the case that you can ignore large chunks of the input you're simply not interested in and merely extract the parts you do care about.
Depending on the details of the problem this may be easier or harder with XSLT, XPath, SAX, XOM, etc. but it's hard to decide that conclusively without knowing more about the documents you're working with.


Elliotte Rusty Harold<br />Author of <a href="http://cafe.elharo.com/web/refactoring-html/" target="_blank" rel="nofollow">Refactoring HTML</a>
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Thanks for the reply!
From what I understand, I would use XSLT if I wanted to convert their XML into my own, right?
These documents aren't particularly long (3 pages, maybe), but they have nested data in them. I looked into SAX because it's supposed to be efficient, but it sure looked like a TON of code to import a dozen tags. DOM looked a bit more difficult and then I kinda ran out of time for the project (this was a few months ago). Now that I'm back on it, I was hoping to find a magic bullet
I don't know anything about XPath or XOM though....
If you're curious enough (and you're probably not!), you could see an example of one site's XML HERE
Thanks again for the advice!
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by T Manning:
From what I understand, I would use XSLT if I wanted to convert their XML into my own, right?
Yep. So you would have three .xsl files and some Java code that figures out which of the three XSL documents it should use, and then does something like this in order to convert the XML into a common vocabulary.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
My understanding is still limited, but couldn't one simply read each of the XML files into DOM and write conversion routines for each of them to turn them into my own javabean structure? Would that be simpler than dealing with XSLT?
The idea of writing XSLT files to convert XML that has nested data structures into my own schema kinda scares me - especially since I don't need my own schema. I intend to read info into my program, and simply serialize the beans to XML with XML Encode or something.
I realize there's no magic bullet, but there has to be one way to do this that is an order of magnitude less work than the others, right?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by T Manning:
My understanding is still limited, but couldn't one simply read each of the XML files into DOM and write conversion routines for each of them to turn them into my own javabean structure? Would that be simpler than dealing with XSLT?
It really depends. With the XSLT approach, you wouldn't need to touch your Java code when one of the schemas changes. With the DOM approach, you wouldn't need to write XSL stylesheets. Which one is easier, is pretty much dependent on the complexity of the schemas.
If you can't make up your mind, just do it. Once you've written some code, take a step back and think again -- does the approach you chose still feel "better", having learned what you've learned.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
By the way, "T M" is not compliant with our naming policy. The previous display name was just fine...
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
My apologies - I forgot about that
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
No problem. Thanks
 
wood burning stoves
 
subject: XML import advice?