Question

I am considerably new to JAXB and Java. I used xjc, and am overriding the custom ns1 ns2 etc namespaces using my own namespacemapper. The java is running through Matlab so it is not formatted exactly like usual java. Any straight Java answers are fine, though, as I should be able to convert it.

Here is what I want:

<String> blah </String>

instead, after marshalling, I am getting:

<String xmlns:ns4="http://www.w3.org/2001/XMLSchema" xsi:type="ns4:string">
blah 
</String>

is there anyway to remove the 'xmlns:ns4' and what not from the element?

The corresponding (matlab)java code is:

a=javax.xml.namespace.QName('String');
c=JAXBElement(a, inner_class, 'blah');

where c is later added to the list that contains the String element.

jxb = JAXBContext.newInstance('mypackage');
jaxbMarshaller = jxb.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty('com.sun.xml.internal.bind.namespacePrefixMapper', MyCustomMapper());
jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, 'http://thelocationofmyschema.com')
jaxbMarshaller.marshal(jaxbclassinstance, file);

My XML is functional, but just to be neat/consistent I'd like to remove that xmlns junk if it is relatively simple.

Thank you! this is my first stackoverflow question ever, sorry if it is not up to par.

UPDATE

I solved this. Two things were required: instead of 'blah', i made

 blah=java.lang.String('blah')

something I forget to do for java within matlab. Secondly, when making the JAXBElement, instead of using an inner_class variable, if i use blah.getClass(), it prints out without any qualifications. I realize now this was more of a mashalling question than a QName question.

Was it helpful?

Solution

As requested, posting my solution as an answer:

UPDATE

I solved this. Two things were required: instead of 'blah', i made

blah=java.lang.String('blah') something I forget to do for java within matlab. I believe when entering just 'blah' something went wrong in converting matlab string to java string.

Secondly, when making the JAXBElement, instead of using an inner_class variable--which was created using

classlist = class.getClass.getClasses();
inner_class = classlist(1);

i use blah.getClass() (where blah is a variable for a string).

this prints out without any qualifications. I realize now this was more of a mashalling question than a QName question.

Ask me why any of these are necessary, and I will not have an answer for you :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top