Customize names of generated JAXB classes

I have a peculiar requirement. We have been using JAXB1.0 in our project to parse XMLs. External binding files were used to provide proper names for the generated JAXB classes. Now, I have an xml that looks something like this:
//sample xml. Original format is much bigger
<b>
<MasterTag>
<attr id="123">Value1</attr>
<attr id="736">Value2</attr>
<attr id="900">value3</attr>
</MasterTag>
</b>
There's a property file which has key value pairs for the attr id
Eg:
<b>
123=CompanyID
736=CompanyAddress
900=CompanyPinCode
</b>
Is it possible to generate JAXB classes names based on the attr id value? These values finally need to persisted in the db. JAXB is the preferred technology.
Any other ideas are also welcome. Thanks.

I might be misunderstanding your question, but if you control the single large schema, it would be simple to define the things that you want to treat specially as either individual elements or complexTypes inside the single schema file. Using psuedo-schema, say you have something like this now:
<xs:element name='Information'>
  <xs:complexType>
    <xs:sequence>
      <xs:element name='Header'>
          <!-- your definition here -->
      </xs:element>
      <xs:element name='Item' maxOccurs='unbounded'>
          <!-- your definition here -->
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>You can change that to:
<xs:element name='Information'>
  <xs:complexType>
    <xs:sequence>
      <xs:element ref='Header'/>
      <xs:element name='Item' maxOccurs='unbounded'>
          <!-- your definition here -->
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name='Header'>
  <!-- your definition here -->
</xs:element>That should produce distinct classes for your Information and Header elements. You similary can break out the Item and Trailer elements.

Similar Messages

  • Problem Generating JAXB Classes

    I am using Eclipse (Enterprise Pack) and want to generate JAXB classes from a schema. I right click on the schema, select Generate JAXB classes - fill in package name, next and get the error:
    java.lang.NoClassDefFoundError: com/sun/tools/xjc/XJCFacade
    Caused by: java.lang.ClassNotFoundException: com.sun.tools.xjc.XJCFacade
         at java.net.URLClassLoader$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
         at java.lang.ClassLoader.loadClass(Unknown Source)
    Exception in thread "main"
    Am I missing a library?
    Steve Watson

    The web service project will add additional server runtime libraries to the classpath thus resolving the jaxb dependencies, in the case of a dynamic web project, you'll have to manage that with the JPA facet and eclipselink, or by adding the Moxy jars seperately. However, if you want to use the target runtime libraries create a user-library and reference the following server libraries: (from Server Install Dir\modules) glassfish.jaxb_"version".jar, glassfish.jaxb.xjc_"version".jar, javax.activation_"version".jar, javax.xml.bind_"version".jar, javax.xml.stream_"version".jar.

  • Generating JAXB classes

    Hi,
    My xml: http://www.javaexperience.com/catalog.xml
    I have an xml for which I want to generate JAXB classes. Unfortunately I don't have access to xsd for this xml.
    Could someone please share steps to generate JAXB classes for a xml. What I have tried is:
    1) Generate the xsd from this xml using XML to XSD Generater Online - xmlGrid.net
    2) Use the Maven JAXB plugin "org.jvnet.jaxb2_commons"
    The problem I faced with my approach was that:
    Since the xml has root element and one of the child element as same name, the JAXB processor fails on reaching the child "Catalog" tag. Even though it had successfully processed the root "Catalog" tag.
    Any pointers would of great help.

    The web service project will add additional server runtime libraries to the classpath thus resolving the jaxb dependencies, in the case of a dynamic web project, you'll have to manage that with the JPA facet and eclipselink, or by adding the Moxy jars seperately. However, if you want to use the target runtime libraries create a user-library and reference the following server libraries: (from Server Install Dir\modules) glassfish.jaxb_"version".jar, glassfish.jaxb.xjc_"version".jar, javax.activation_"version".jar, javax.xml.bind_"version".jar, javax.xml.stream_"version".jar.

  • How do I unmarshal to only a portion of generated JAXB classes?

    I have an XML schema that is laid out something like this:
    <Information>
    <Header/>
    <Item> (Unbounded)
    <Trailer/>
    </Information>
    I receive files that have thousands of items in them. Because of the size of the files, I must use SAX to parse the file.
    I need to unmarshal each file and store the results in a database after following some conditional logic. It is not practical to unmarshal a 500 MB file.
    What I have been doing is using a SAX DefaultHandler to strip out each <Item> element.
    I had to create a separate schema for just the <Item> element so that I could use JAXB's xjc to generate a set of classes to unmarshal each <Item> element to.
    This works great, but it is a hassle to maintain. You see, I also need to unmarshal the <Header> element. So I have to create a separate schema for that as well. So then I end up with two sets of JAXB classes, with lots of duplication. Plus, if I have any complex types that are referenced in both the <Header> and the <Item>, then I have to include those in each sub schema.
    What I want to know is:
    Is it possible to just generate ONE set of JAXB classes for the main schema, and then unmarshal the stripped out <Item> element to just the corresponding Item in the generated JAXB classes?
    This is a complicated topic, and I hope I've been clear. Basically, I have to create these <Header> and <Item> schemas by hand every time the main schema changes, and I want to get away from that.

    I might be misunderstanding your question, but if you control the single large schema, it would be simple to define the things that you want to treat specially as either individual elements or complexTypes inside the single schema file. Using psuedo-schema, say you have something like this now:
    <xs:element name='Information'>
      <xs:complexType>
        <xs:sequence>
          <xs:element name='Header'>
              <!-- your definition here -->
          </xs:element>
          <xs:element name='Item' maxOccurs='unbounded'>
              <!-- your definition here -->
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>You can change that to:
    <xs:element name='Information'>
      <xs:complexType>
        <xs:sequence>
          <xs:element ref='Header'/>
          <xs:element name='Item' maxOccurs='unbounded'>
              <!-- your definition here -->
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <xs:element name='Header'>
      <!-- your definition here -->
    </xs:element>That should produce distinct classes for your Information and Header elements. You similary can break out the Item and Trailer elements.

  • Generating JAXB classes from an XSD

    Hi All,
    Can anyone please tell me how to generate Jaxb classes for xsd in eclipse , or in ant .
    Thanks and Regards

    Try here as well
    https://jaxb.dev.java.net/

  • Generating JAXB classes from XSD

    I am generating java classes using JAXB binding from XSDs. I organized the classes into 2 packages say package1 and package2
    The XSD to create the package2 is dependent on package1. So I do an include package1.xsd in package2.xsd. With this I am running into an issue with JDeveloper while generating the classes. When I generate classes using package2.xsd, all package1.xsd classes are also generated in the same package. So, I have to manually delete these classes from package1.xsd and add import statements from package1 where required.
    Is there a solutiion/work around available for suppressing duplicate class generation? Any help would be greatly appreciated.

    Try here as well
    https://jaxb.dev.java.net/

  • Mapping Generated JAXB Classes to JAVA Classes

    Hi,
    i�m writing a framework to allow building swing components from xml files. So i use JAXB to generate the stubs and then in
    my JAVA app i do an unmarshal process to get the jaxb elements. Then i process all the elements in the jaxb tree and
    i create the corresponding swing components (i.e. JPanel, GridLayout, JButton, JTextField, etc). This is very annoying,
    since each component has different properties and this leads to many if statements which just make my code messy
    and not reusable.
    Could someone give me an advice of how i could make this solution more elegant?
    Thanks for any ideas,
    Chris

    Hi,
    Can you please let me know if you found a solution? I have the same exact problem. I have pretty solid collection of similar questions in the NET, but not a single answer yet... Keep searching...
    Thanks!
    Rouben.
    P.S can you please CC your answer to [email protected]?

  • Customization of generated Java class names

    Hi,
    I could not find any documentation on how to customize names of the generated
    Java classes/types. In Castor, you can do it with a simple mapping, is it possible
    with XMLBeans?
    Thanks,
    Marina

    Hi, Steve,
    thanks for your reply!
    I have tried to add an xsdconfig file, but when I run the following command:
    C:\Java\XMLBeans\xkit\bin\scomp -src . -srconly order.xsd
    with the xsdconfig file in the current directory , nothing happens - the xsdconfig
    file is ignored and Java files are generated in exactly same way as they were
    without the xsdconfig file...
    Here is the content of my xsdconfig file - it is very simple, for test purposes:
    <xb:config xmlns:order="http://order"
    xmlns:xb="http://www.bea.com/2002/09/xbean/config">
    <xb:namespace uri="http://order">
    <xb:package>neptune.neptunelib</xb:package>
    </xb:namespace>
    <xb:qname name="order:order" javaname="OrderDO"/>
    <xb:qname name="order:shippingID" javaname="ShippingInfoDO"/>
    <xb:qname name="order:addressInfo" javaname="AddressInfoDO"/>
    <xb:qname name="order:destination" javaname="DestinationDO"/>
    </xb:config>
    I'm not sure about the namespace part - currently, my order.xsd has no namespace
    or URI assigned. I asked BEA's customer support about this and the answer was:
    'use anything, the URI does not matter'. Well, I did use "anything" but I'm not
    sure this is correct...
    Thanks,
    Marina
    "Steve Traut" <[email protected]> wrote:
    Marina -- You can do this by creating an xsdconfig file and including
    it
    when you compile your XSD files. The xsdconfig file maps schema types
    to
    generated Java type names.
    If you're using WebLogic Workshop, the following topic should give you
    what
    you need:
    http://edocs.bea.com/workshop/docs81/doc/en/workshop/guide/howdoi/howGuideXMLBeansTypeNaming.html
    If you're using Ant, I can provide a topic that will be available soon.
    Please send email if you'd like information on the Ant task.
    Steve
    "Marina Popova" <[email protected]> wrote in message
    news:3f8c468c$[email protected]..
    Hi,
    I could not find any documentation on how to customize names of thegenerated
    Java classes/types. In Castor, you can do it with a simple mapping,is it
    possible
    with XMLBeans?
    Thanks,
    Marina

  • JAXB class conflict error

    I am new to JAXB.
    I am using external customization file to create JAXB classes.
    I have one XSD file which includes another XSD. Both of them have the same element name and the classes are getting created in the same folder. I have tried each and every option of the options mentioned in the fix collides examples of JAXB sun tutorial without any success.
    Please guide me on this.
    This is the following error:
    compile:
    [echo] Compiling the schema...
    [xjc] C:\Sun\jwsdp-1.6\jaxb\samples\acord-dri\gen-src\primer.po is not found and thus excluded from the dependency
    check
    [xjc] Compiling file:/C:/Sun/jwsdp-1.6/jaxb/samples/acord-dri/Aon_Acord-Repository_v-1-0-1.xsd
    [xjc] [ERROR] Multiple <schemaBindings> are defined for the target namespace "http://www.ACORD.org/Standards/Acord
    MsgSvc/1.1.0"
    [xjc] line 129 of binding.xjb
    [xjc] [ERROR] Another <schemaBindings> is defined here
    [xjc] line 98 of binding.xjb
    [xjc] [ERROR] A class/interface with the same name "primer.po.SenderAonAcord" is already in use. Use a class custo
    mization to resolve this conflict.
    [xjc] line 265 of Acord-Repository_v-1-0-1.xsd
    [xjc] [ERROR] (Relevant to above error) another "SENDERAonAcord" is generated from here.
    [xjc] line 217 of Aon_Acord-Repository_v-1-0-1.xsd
    [xjc] [ERROR] This error is caused because on Windows you cannot have both "SENDERAonAcord.java" and "SenderAonAco
    rd.java" in the same directory.
    [xjc] unknown location
    [xjc] failure in the XJC task. Use the Ant -verbose switch for more details

    This is because of name conflicts in the xsd. If the <xsd:element> name is same as some <xsd:complexType> and that is referenced from the element this happens. If you provide different names, it will solve the problem
    Regards,
    Sushil

  • JAXB classes generation

    Hi all,
    I have 4 XMLSchema files and need to generate JAXB classes out of them. But most of the nodes like header parts of request and response of messages are same in structure. If I generate classes in 4 different packages how can I separate out the common node .java classes so that setting / getting repetition is avoided in my application.
    Thanks in advance.
    JoyBoy

    There are some good examples here:
    http://java.sun.com/webservices/jaxb/users-guide/jaxb-using.html

  • JAXB 1 : generating simpleTypes classes

    Sorry, I posted an uncompleted message.
    Hello, I'm new to JAXB technology.
    Since I'm working with j2sdk 1.4 I c'ant use JAXB 2, so I'm using JAXB 1.0.6.
    When I call the xjc ant task, my java classes are generated... buy simple types do not generate any java class.
    Let's say I have the following xsd:
    <xs:complexType name="EXAMPLE">
    <xs:sequence>
    <xs:element name="market-type" type="po:market-type"/>
    </xs:sequence>
    </xs:complexType>
    <xs:simpleType name="MARKET-TYPE">
    <xs:restriction base = "xs:string">
    <xs:enumeration value = "PRIM"/>
    <xs:enumeration value = "SECOND"/>
    </xs:restriction>
    </xs:simpleType> After xjc compilation I have a EXAMPLE Java class, but its element "market-type" is a String and not a MARKET-TYPE as I expected.
    Is it normal? Can I customize the output in order to generate java classes for simpleTypes?

    The base data type of the simpleType MARKET-TYPE is xsd:string.

  • JAXB to generate java classes for XSD.

    Hi
    I have a XSD, which is importing other couple of xsds in it, tried to generate java classes using xjc, it is throwing error.
    C:\vittal\Project\received\development-1\da_xsd>xjc -p com daAuthoring.xsd
    parsing a schema...
    [ERROR] Property "Alt" is already defined. Use <jaxb:property> to resolve thi
    s conflict.
      line 42 of file:/C:/vittal/Project/received/development-1/da_xsd/commonElement
    s.xsd
    [ERROR] The following location is relevant to the above error
      line 2205 of file:/C:/vittal/Project/received/development-1/da_xsd/daCommonEle
    ments.xsd
    Failed to parse a schema.Please let me know how to fix this issue.

    Thanks for information,
    I do understand xml well, and having basic knowledge on XSD.
    1. can i generate a java classes for a xsd which is including other XSDs.
    2. how to overwrite the issue, using annotation, point me to a location where i will get more information, that will be great help.
    here is my xsd.
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:dctm="http://www.documentum.com" elementFormDefault="qualified">
         <xs:import namespace="http://dita.oasis-open.org/architecture/2005/" schemaLocation="ditaarch.xsd"/>
         <xs:import namespace="http://www.documentum.com" schemaLocation="dctmAttrs.ent.xsd"/>
         <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
         <xs:group name="alt">
              <xs:sequence>
                   <xs:element ref="alt"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="desc">
              <xs:sequence>
                   <xs:element ref="desc"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="title">
              <xs:sequence>
                   <xs:element ref="title"/>
              </xs:sequence>
         </xs:group>
         <!-- Elements in tblDecl.mod -->
         <xs:group name="colspec">
              <xs:sequence>
                   <xs:element ref="colspec"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="entry">
              <xs:sequence>
                   <xs:element ref="entry"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="row">
              <xs:sequence>
                   <xs:element ref="row"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="tbody">
              <xs:sequence>
                   <xs:element ref="tbody"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="tgroup">
              <xs:sequence>
                   <xs:element ref="tgroup"/>
              </xs:sequence>
         </xs:group>
         <xs:group name="thead">
              <xs:sequence>
                   <xs:element ref="thead"/>
              </xs:sequence>
         </xs:group>
    </xs:schema>

  • JAXB problem generating java classes

    I'm doing some integration and have received a schema from the vendor the other day. When I try to generate java classes with the jaxb compiler I get this output
    parsing a schema...
    [ERROR] Property "Value" is already defined.
      line 14 of jar:file:/C:/win32app/Java/jdk6/lib/tools.jar!/com/sun/xml/internal/xsom/impl/parser/datatypes.xsd
    [ERROR] The following location is relevant to the above error
      line 384 of file:/C:/code/sca-ecr.xsd
    Failed to parse a schema.
        <xsd:complexType name="options">
            <xsd:sequence>
                <xsd:element name="option" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:simpleContent>
                            <xsd:extension base="xsd:string">
    (Line 384)                  <xsd:attribute name="value" type="xsd:string"/>
                            </xsd:extension>
                        </xsd:simpleContent>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>So, googling about this I came across these to articles that almost describes my problem.
    http://weblogs.java.net/blog/kohsuke/archive/2005/05/compiling_mathm_1.html
    https://jaxb.dev.java.net/guide/Dealing_with_errors.htmlAdding the this code did not help. Downloading JAXB 2.1.4 did not help, adding the -extension parameter still same result.
            <xsd:annotation>
              <xsd:appinfo>           
                <jaxb:property name="someAttribute" />
              </xsd:appinfo>
            </xsd:annotation>      Why is jaxb failing ? Could it be that and attribute is not allowed to be called "value" ?
    Any answers will do
    regards abq

    Well, after hours of digging I found a solution which actually was right in front of me the whole time.
    This is how I edited the schema.
        <xsd:complexType name="options">
            <xsd:sequence>
                <xsd:element name="option" maxOccurs="unbounded" >
                    <xsd:complexType>
                        <xsd:simpleContent>
                            <xsd:extension base="xsd:string">
                               <xsd:attribute name="value" type="xsd:string" >
                                  <xsd:annotation><xsd:appinfo>
                                    <jaxb:property name="realValue" />
                                  </xsd:appinfo></xsd:annotation>
                                 </xsd:attribute>
                            </xsd:extension>
                        </xsd:simpleContent>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>Before, I placed the <xsd:annotation> tag efter the first line but inside the <xsd:attribute> made much better if you would like it to work.
    When marshling an @XmlRoot object will produce valid xml code. So even though that the server have a different xml schema, they will be able to talk to each other.
    abq

  • JAXB not generating correct class definition

    Hi,
    I am using JAXB 2.0 and the JAXB plugin for Eclipse 3.x.
    The problem I have is that when I run XJC on my XSD file the class definition for
    a particular class is not what I am expecting. The class only defines 1 get method instead of three. While I am expecting to see getX getY and getZ I am seeing only 1 method getXAndgetYAndgetZ. Here is a snippet of the XSD file:
         <xs:element name="Sql">
              <xs:complexType>
                   <xs:sequence minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="Query"/>
                        <xs:element ref="Table" maxOccurs="unbounded"/>
                        <xs:element name="Database" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                   </xs:sequence>
              </xs:complexType>
         </xs:element>I have defined the Table element in the XSD.
    In this case my generated Sql class has the following method:
    public List<JAXBElement<String>> getQueryAndTableAndDatabase() {
            if (queryAndTableAndDatabase == null) {
                queryAndTableAndDatabase = new ArrayList<JAXBElement<String>>();
            return this.queryAndTableAndDatabase;
        }Is there a problem with my XSD file or am I omitting a parameter when running XJC? Any help greatly appreciated

    LDAPLoginModule uses the jackrabbit UserManagerImpl to create user's in CRX (CQ), not the CQ5 implementation which is disappointing. Have to manually add the sling:resourceType.

  • Why does Flash generate invalid class names for library items?

    I imported a bunch of bitmaps (PNGs).  The library symbol names all included the full file name and its extension.  When I check "export for actionscript" it generates a class name based on the symbol name, and it includes the ".png", even though that's invalid in a class name.
    When I select multiple bitmaps and choose properties and export for actionscript, it has a note saying that classnames will be automatically generated for all items, but it does the same thing... including the ".png" in all the class names.  Is this a problem?  Wouldn't it save time if Flash would automatically strip the file extension from the instance name of bitmap library symbols when generating class names?

    Thank you Nipun.  That is exactly the behavior I would expect, and I can confirm that in the trial of Flash CC it generates valid class names by removing the file extension.  For example, a file named "3ball.test.png" will receive an auto-generated class name of "_3ball_test", so it really does ensure the class name is valid (meeting AS3 class name syntax)
    kglad, what I'm saying is that an image file name like "ball.png" is not a valid "class name", because it contains a period, which is reserved as a package delimiter.  I know the runtime can handle it as a valid "fully-qualified class name", but it's misleading because it's actually creating a class named "png" in package "ball", BUT it's understood that the auto-generated name is not supposed to be specifying any package, hence the misleading specification.  The class would have to be instantiated like this: "import ball.png; var bd:BitmapData = new png();" or "import ball.*; var bd:BitmapData = new png();"  You would also then have to import every single class individually and always use their fully qualified names, because you'd technically have a bunch of classes all named "png" inside a hundred different packages.  It's just not acceptable.
    The other problem that occurred in CS6 is if you loaded a bunch of files whose names started with numbers, then when you select them all, choose properties, and use the batch properties window to export them for actionscript, I noticed that such images would be auto-assigned generic names like "Class4" as it would realize the default auto-generated class name would be invalid.  This problem has been fixed beautifully in Flash CC.
    Nipun, it would actually be very helpful to have a little more control over the class naming conventions.  For example, if I wanted all my BitmapData classes to exist in an "images" folder or package, it would be helpful if in the batch properties window I could specify a package name for all the auto-generated class names.  When importing hundreds of images, it's very tedius to have to edit them individually and I don't always have time to write a JSFL script.  There's room in that window to add a text field for a package name.  Thanks!

Maybe you are looking for