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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:52+00:00 2026-05-23T16:02:52+00:00

Im working on a java project where i need to read some objects from

  • 0

Im working on a java project where i need to read some objects from an XML file, do some processing which will alter the object´s atributes and then write the Object to another XML file. For that purpose, i am using JAXB with its marshalling and unmarshalling capabilities, each of them in a method, like this:

private MyObject unmarshallXMLFile(String file) {
    MyObject t=null;
    try {
        jc = JAXBContext.newInstance("foo.bar");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        unmarshaller.setSchema(sf.newSchema(new File("MySchema.xsd")));
        t = (Task) unmarshaller.unmarshal(new File(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return t;
}

private void marshallXMLFile(String file) {
    task.setReplay(Boolean.TRUE);
    Marshaller marshaller;
    try {
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(task, new FileOutputStream(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

The problem is that automatically-generated namespace prefixes such as ns2 or ns3 keep appearing in the output file, and then when i want to reuse this files with the unmarshallXMLFile method (i will be using the output files as input again later) it wont get validated against the schema, and throws a org.xml.sax.SAXParseException. Here are the files i wrote:

XML Schema: MySchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/MySchema"
    xmlns:spm="http://www.example.org/MySchema"
    elementFormDefault="qualified"
    attributeFormDefault="qualified">

    <element name="task" >
        <complexType>
            <sequence>
                <element name="replay" type="boolean" default="false"/>
                <element name="threads" type="spm:spThread" maxOccurs="unbounded" minOccurs="1" />
            </sequence>
        </complexType>
    </element>

    <complexType name="spThread">
        <sequence>
            <element name="SPThreadID" type="int" />
            <element name="durtime" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="minexecutions" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="numThreads" type="int" />
            <element name="procedures" type="spm:procedure" minOccurs="1"
                maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="procedure">
        <sequence>
            <element name="id" type="int" minOccurs="1" />
            <element name="name" type="string" minOccurs="1" />
            <element name="weight" minOccurs="1">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                        <maxInclusive value="100" />
                    </restriction>
                </simpleType>
            </element>
            <element name="parameterPool" type="spm:parameter" nillable="true"
                minOccurs="0" maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="parameter">
        <sequence>
            <element name="name" type="string" minOccurs="1" />
            <element name="dataType" type="spm:parameterDataType" default="integer"/>
            <element name="parmType" type="spm:parameterType" default="in"
                minOccurs="0" />
            <element name="minValue" type="string"/>
            <element name="maxValue" type="string"/>
            <element name="value" type="string"/>
        </sequence>
    </complexType>

    <simpleType name="parameterDataType">
        <restriction base="string">
            <enumeration value="integer" />
            <enumeration value="varchar" />
            <enumeration value="char" />
        </restriction>
    </simpleType>

    <simpleType name="parameterType">
        <restriction base="string">
            <enumeration value="in" />
            <enumeration value="out" />
            <enumeration value="in_out" />
        </restriction>
    </simpleType>

</schema>

input file:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<task xmlns="http://www.example.org/MySchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/MySchema MySchema.xsd ">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>                
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>                                
            </parameterPool>
        </procedures>
    </threads>
</task>

output file (without any processing whatsoever: just unmarshalling and marshalling back with the methods mentioned earlier)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:task xmlns="http://www.example.org/MySchema" xmlns:ns2="http://www.example.org/MySchema.xsd">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>
            </parameterPool>
        </procedures>
    </threads>
</ns2:task>

exception (when using the ouput file again as an input):

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:326)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:500)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:148)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:153)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:180)
    at Test.main(Test.java:48)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.XMLSchemaValidatorHandler.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:85)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
    ... 6 more

I´ve been reading about the subject and tried loads of related answers, but none of them seems to remove the prefixes. I went trough this guide but the version of jaxb i am using does not support NamespacePrefixMapper. I tried using annotations as described here to configure the prefixes but that wouldnt work.

Maybe there is a way of getting rid of this namespace prefixes: all the forums, answers and discussions i´ve found talk about customizing this prefixes, i just want to get rid of them. But somehow it makes me think that i´m missing something in both my input file and schema. Are they well written? I would say that right there is the problem, because it is the first time i work with xml and xsd at this depth, and what i´ve done is only based on what i´ve found online. Any tips on improving the xml and xsd designs would be highly appreciated

should i be using some kind of prefixes in the input file or in the schema so the JAXB framework wont generate random prefixes at time of marshalling?

thanks in advance, i hope you guys can help me.

—

Thank you very much for the answer. In that way i can use the NamespacePrefixMapper. However, when i use it, my code keeps throwing exceptions when i run it:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name javax.xml.bind.Messages, locale de_DE
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:863)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:832)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
    at javax.xml.bind.Messages.format(Messages.java:47)
    at javax.xml.bind.Messages.format(Messages.java:36)
    at javax.xml.bind.PropertyException.<init>(PropertyException.java:99)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:349)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
    at Test.main(Test.java:95)

I found that it has to do something with a .properties file: i am not using anything like it, i havent changed anything.

  • 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-23T16:02:52+00:00Added an answer on May 23, 2026 at 4:02 pm

    Well, after some research, i tried using the @XMLElement tag on every attribute of the classes i am trying to serialize, specifying clearly what my namespace was, and using the same one on every attribute:

    @XmlElement(required = true, name="myObjectPool", namespace="http://www.example.org/StoredProceduresSchema")
        protected List<MyObject> myObjectPool;
    

    It worked flawlessly: no more weird namespaces in the marshalled file.

    I wanna thank for his answer: i tried that as well, but i got a weird language bundle related exception. Im glad this simpler approach solved the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coming from Java and am currently working on a C# project. What
I'm working on a project in Java where I need to display a powerpoint
I am working on a Java project using Netbeans for the first time. I
I have been working on a Java project for a class for a while
I'm currently working on a Java project that is emitting the following warning when
I am working in Java on a fairly large project. My question is about
I'm a longtime Java programmer working on a PHP project, and I'm trying to
I'm working on a web app project (in java; not that it matters) and
I'm working on a project where we're using a Java applet for part of
I'm working on a small UML editor project, in Java, that I started a

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.