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 6251403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:36:26+00:00 2026-05-24T13:36:26+00:00

I am trying to marshall data within an object into an xml file based

  • 0

I am trying to marshall data within an object into an xml file based on a defined schema. However when I print out the xml file, I recieve extra annotations on the xml tags. Is there any way to get rid of the extra namespace annotation (i.e. ns2)

This is an example of the xml I receive from marshalling.

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns:ns2="http://www.something.com/something">
    <ns2:food>steak</ns2:food>
    <ns2:beverage>water</ns2:beverage>
</root>

What I want is something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns="http://www.something.com/something">
    <food>steak</food>
    <beverage>water</beverage>
</root>

This is what my Java code is doing:

            JAXBContext context = JAXBContext.newInstance("com.schema");
            JAXBElement<FoodSchema> element = new JAXBElement<FoodSchema>
                (new QName("FoodSchema"), Food.class, foodSchema);
            Marshaller marshaller = context.createMarshaller();
            OutputStream os = new FileOutputStream(object.getFilePath());
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
            marshaller.marshal(element, os);

Any help is much appreciated! Thanks!

  • 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-05-24T13:36:29+00:00Added an answer on May 24, 2026 at 1:36 pm

    By adding a namespace URI (“http://www.something.com/something”) to the QName used to construct the JAXB element, and leveraging the package level @XmlSchema annotation will get you the namespace qualification that you are looking for:

    package-info

    @XmlSchema(
        namespace="http://www.something.com/something", 
        elementFormDefault=XmlNsForm.QUALIFIED)
    package forum7014746;
    
    import javax.xml.bind.annotation.*;
    

    Food

    package forum7014746;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Food {
    
        private String food;
        private String beverage;
    
        public String getFood() {
            return food;
        }
    
        public void setFood(String food) {
            this.food = food;
        }
    
        public String getBeverage() {
            return beverage;
        }
    
        public void setBeverage(String beverage) {
            this.beverage = beverage;
        }
    
    }
    

    Demo

    package forum7014746;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.Marshaller;
    import javax.xml.namespace.QName;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jaxbContext = JAXBContext.newInstance(Food.class);
    
            Food foodSchema = new Food();
            foodSchema.setFood("steak");
            foodSchema.setBeverage("water");
    
            JAXBElement<Food> element = new JAXBElement<Food> (new QName("http://www.something.com/something","FoodSchema"), Food.class, foodSchema);
    
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(element, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <FoodSchema xmlns="http://www.something.com/something">
        <beverage>water</beverage>
        <food>steak</food>
    </FoodSchema>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to unmarshall data from an XML object in Java using SimpleXML.
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to embed a PDF file into a Word document using the OLE
Getting a 'marshal data too short' error when trying to install the mysql gem
I am trying to use JAXB's introspection to marshall and unmashall some existing domain
I'm trying to marshall unsigned char** (which is in a C++ interface) in order
I was trying to use a struct to parse socket data when implement a
I'm trying to send a network packet of data to a hardware device. I
I need to validate my JAXB objects before marshalling to an XML file. Prior
I'm having a hard time figuring out the correct schema (to validate the structure

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.