• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAXB namespace use in XML files

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for reading a XML that its elements have namespace:


 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic