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

  • Home
  • SEARCH
  • 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 9173177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:31:42+00:00 2026-06-17T16:31:42+00:00

I’ have simple question about JAXB. Here is the sample code: //setter that has

  • 0

I’ have simple question about JAXB. Here is the sample code:

   //setter that has input JAXBElement
   b.setBIC(JAXBElement<String> value);

How can I initialize the input element, that uses String from other object?

  • 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-17T16:31:44+00:00Added an answer on June 17, 2026 at 4:31 pm

    You can create an instance of JAXBElement directly or if you generated your Java model from an XML schema use a convience method on the generated ObjectFactory class.

    package org.example.schema;
    
    import javax.xml.bind.*;
    import javax.xml.namespace.QName;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance("org.example.schema");
    
            Root root = new Root();
    
            QName fooQName = new QName("http://www.example.org/schema", "foo");
            JAXBElement<String> fooValue = new JAXBElement<String>(fooQName, String.class, "FOO");
            root.setFoo(fooValue);
    
            ObjectFactory objectFactory = new ObjectFactory();
            JAXBElement<String> barValue = objectFactory.createRootBar("BAR");
            root.setBar(barValue);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    schema.xsd

    The above demo code is based on a Java model generated from the following XML schema. The reason you get a JAXBElement<String> property in the first place is when you have an element that is both nillable="true" and minOccurs="0".

    <?xml version="1.0" encoding="UTF-8"?>
    <schema 
        xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/schema" 
        xmlns:tns="http://www.example.org/schema" 
        elementFormDefault="qualified">
        <element name="root">
            <complexType>
                <sequence>
                    <element name="foo" type="string" minOccurs="0" nillable="true"/>
                    <element name="bar" type="string" minOccurs="0" nillable="true"/>
                </sequence>
            </complexType>
        </element>
    </schema>
    

    Root

    The following class was generated from schema.xsd and contains properties like the one in your question.

    package org.example.schema;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {"foo","bar"})
    @XmlRootElement(name = "root")
    public class Root {
    
        @XmlElementRef(name = "foo", namespace = "http://www.example.org/schema", type = JAXBElement.class)
        protected JAXBElement<String> foo;
        @XmlElementRef(name = "bar", namespace = "http://www.example.org/schema", type = JAXBElement.class)
        protected JAXBElement<String> bar;
    
        public JAXBElement<String> getFoo() {
            return foo;
        }
    
        public void setFoo(JAXBElement<String> value) {
            this.foo = value;
        }
    
        public JAXBElement<String> getBar() {
            return bar;
        }
    
        public void setBar(JAXBElement<String> value) {
            this.bar = value;
        }
    
    }
    

    ObjectFactory

    Below is the generated ObjectFactory class that contains convenience methods for creating the instances of JAXBElement.

    package org.example.schema;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    import javax.xml.namespace.QName;
    
    @XmlRegistry
    public class ObjectFactory {
    
        private final static QName _RootFoo_QNAME = new QName("http://www.example.org/schema", "foo");
        private final static QName _RootBar_QNAME = new QName("http://www.example.org/schema", "bar");
    
        public Root createRoot() {
            return new Root();
        }
    
        @XmlElementDecl(namespace = "http://www.example.org/schema", name = "foo", scope = Root.class)
        public JAXBElement<String> createRootFoo(String value) {
            return new JAXBElement<String>(_RootFoo_QNAME, String.class, Root.class, value);
        }
    
        @XmlElementDecl(namespace = "http://www.example.org/schema", name = "bar", scope = Root.class)
        public JAXBElement<String> createRootBar(String value) {
            return new JAXBElement<String>(_RootBar_QNAME, String.class, Root.class, value);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.