aspose file tools*
The moose likes XML and Related Technologies and the fly likes JAXB namespace use in XML files 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 "JAXB namespace use in XML files" Watch "JAXB namespace use in XML files" New topic
Author

JAXB namespace use in XML files

Seg Violet
Greenhorn

Joined: May 20, 2011
Posts: 4
I have a JAXB namespace formatting problem with XML files: The XML file format that JAXB accepts is different from the format I need to follow, and I'm not able to read/write xml files in my format.

I have 2 schema files that I'm using, and have been able to create the appropriate java files for JAXB. I can even read and write XML files such as the one below:

Unfortunately, the above format doesn't match the pre-existing format convention that I have to continue following:
this format has been used by other, non-Java code that we're still using, and other users use this file in the current format, so I can't change the format suddenly. So there really isn't a way for me to just change the current xml file format for my needs.

As a result, I need to make my xml files look like the following:

With this format, JAXB correctly identifies the number of entries in coordmap (2 point targets
and one polygon) and loads their names, but the site and coordinate information is not loaded (I get Null/zero entries). I get no error messages that there was a problem -- I only realized what was going on when I tried to write an xml file and saw the format change. The above format is purportedly following the W3C format conventions, which JAXB is supposed to support, but I'm not an expert on that and I could be wrong. Regardless, I have to stick with that format.

Is there a way that I can get JAXB to handle my format or am I better off starting with something else/from scratch? I don't mind modifying the JAXB code if I can get at it the right part. If I'm better off writing my own, specialized parser, I'd rather get started now rather than burn time on trying to force JAXB to do it.

Any thoughts?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

To me it doesn't look too hard to write XSLT code to convert either of those formats to the other format. Have you considered that possibility?
Seg Violet
Greenhorn

Joined: May 20, 2011
Posts: 4
Why no, I hadn't! That sounds a lot easier than digging into JAXB's entrails to modify it. I will definitely look into it.
Will XLST hard to set up if you're new to it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

No setup required, it's already part of the standard Java API.

However it is a programming language and it does take a bit of learning (still easier and more practical than trying to hack JAXB, though, I think). This probably won't mean much to you right now, but for this task I suggest starting from the identity transformation and then adding templates for the specific translations you need to do. For example you need to convert a <site> element to an <ns2:site> element (or the other way around, whichever it is), so you would write a template which creates an <ns2:site> elements whenever it matches a <site> element.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 541
    
    3
With this format, JAXB correctly identifies the number of entries in coordmap (2 point targets
and one polygon) and loads their names, but the site and coordinate information is not loaded (I get Null/zero entries).

Before you jump to that conclusion, have you envisaged the possibility that you actually have not used the correct way to get the site and coordinate information? How do you get to them in your code?
Seg Violet
Greenhorn

Joined: May 20, 2011
Posts: 4
Yes, there is certainly the possibilty that I am not doing something that would would read the site and coordinate with my desired format.
However, what I observed above was done when I used that same piece of test code and swapped between loading in the two different format xml files. One xml file format loads the site and coordinate into the pointTarget and polyObject data structures and the other doesn't (I'm just printing out the values in the structures just to see what got in there in the test code).

If there is something I could do to make JAXB read the other XML file fomat, that would be ideal. but I'm not sure how to change JAXB's xml format preferences (can that be done?).

Paul - thanks for the additional details. I'm definitely going to look into it, although I'm getting tempted just to write a shell script and use regexp to change to format. But having it all contained within the Java API would be better.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 541
    
    3
Rather than referring to somewhere of some model code you follow literally or conceptually, what are the actual two lines of code you get to the site and coordinate information?

I am not sure what is your "desired" format. The meaning seems changing from one context to another. In any case, the one the other party is supplying to you is the superior in design. There is no reason to apply pre-processing of xslt-kind or whatever to bend it to some other format. If you do not want to show the lines, maybe the other party can give you instruction as far as I can see, they know better their job.
mohsen abasi
Greenhorn

Joined: Oct 12, 2011
Posts: 1
for reading a XML that its elements have namespace:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB namespace use in XML files