Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:36:22+00:00 2026-06-15T02:36:22+00:00

How Do I create a simple jaxb Java class to represent the following xml

  • 0

How Do I create a simple jaxb Java class to represent the following xml

<rootelem>
  <myelem name="abc" myatt="true"/>

  <myelem name="def">
     <Key value="newvalue"/>
  </myelem>
  <myelem name="xyz">
     <Key value="42"/>
  </myelem>
</rootelem>

There can be multiple myelem and each myelem can contain multiple key

I do not want to use a xsd

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:36:24+00:00Added an answer on June 15, 2026 at 2:36 am

    Here is a copy of a class we use to convert to/from XML using JAXB using classes and no XSD. (We also use JAXB to generate our XSD).

    EDIT: I just re-read the question. If you’re asking how to generate the Java source from that XML, then you’re going to have to figure that out on your own or use an XSD and use JAXB to convert it to classes. If you already have the class and you want to convert the XML into a Java object, then my code below will work for you.

    package com.mycompany.types;
    
    import java.io.StringReader;
    import java.io.StringWriter;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlTransient;
    
    /**
     * Utility class to make it convenient to marshal and unmarshal the classes
     * generated by JAXB.
     */
    @XmlTransient
    public final class Utility {
    
      //
      // Static initialization
      //
    
      static {
        try {
          JAXB_CONTEXT = JAXBContext.newInstance(TestClass.class);
    // The following fails with a javax.xml.bind.JAXBException.
    // class mycompany.types.TestClass nor any of its super class is known
    // to this context.
    //      JAXB_CONTEXT =
    //        JAXBContext.newInstance("com.mycompany.types",
    //                                Utility.class.getClassLoader());
        }
        catch (Exception e) {
          throw new ExceptionInInitializerError(e);
        }
      }
    
      //
      // Constructors
      //
    
      //
      // Hidden constructor that prevents an object from being created.
      //
      private Utility() {
        // Do nothing.
      }
    
      //
      // Additional methods
      //
    
      /**
       * Unmarshals an XML string to a TestClass object.
       *
       * @param xml the XML string to parse
       * @return the resulting TestClass
       * @throws JAXBException if there are XML errors
       */
      public static TestClass parseTestClass(String xml) throws JAXBException {
        Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        return (TestClass)unmarshaller.unmarshal(new StringReader(xml));
      }
    
      /**
       * Marshals a TestClass object to an XML string.
       *
       * @param testClass
       * @return the resulting XML string
       * @throws JAXBException if there are XML errors
       */
      public static String printTestClass(TestClass testClass) throws JAXBException {
        Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(testClass, writer);
        return writer.toString();
      }
    
      //
      // Attributes
      //
    
      private static final JAXBContext JAXB_CONTEXT;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create simple style.xml file <style name=You.EditText.Style parent=@android:style/Widget.EditText> <item name=android:textColor>@color/colorRed</item> <item name=android:gravity>center</item> </style> But
How to create simple webserver in Java using Eclipse, Tomcat and Jersey i.e steps
I am trying to unmarshall a XML document created from jersey JAXB annotated class.
I create simple Java Application project with src and test folders. src folder consists
when I trying to create simple DB with filestream-Enabled DB with the following Query
I am programming a simple proxy in java: Read XML File Send request to
I tried create simple class which can slide a JPanel like this: +----------+ +------+---+
Is there is Simple way to read and write Xml in Java? I've used
I create simple project using the following environment. 1. Netbeans IDE6.9.1 (File->NewProject->Select JSF2.0 libraries)
I create simple puzzle game and use next for this: I have an class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.