1

I have some jaxb objects that are generated from an xsd I created. Previously when I marshalled the content, the xml output looked like this:

<report xmlns="urn:com-test-person-data:v3">
<person-data>
    .......(Data)
</person-data>
</report>

I have recently change over from coding on a Windows machine to a Mac. Nothing has changed in the jaxb objects I'm using. But now my marshalled output looks like this.

<report>
<person-data>
    .......(Data)
</person-data>
</report>

I've tried doing some digging to see what could've removed the xmlns attribute, but have had no luck. I use that attribute to check what version of my schema is being used, so it's important it's in the outputted xml fragment.

Here's the code snippet where I call the marshaller:

final JAXBContext context = JAXBContext.newInstance(Report.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(reportJaxBObject, sw);

Any ideas?

pnuts
  • 58,317
  • 11
  • 87
  • 139
vogelj
  • 11
  • 3

1 Answers1

0

This issue is usually a result of the package-info.java not getting compiled or packaged with the rest of the model classes. The package-info class is where the @XmlSchema is specified which contains namespace information.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • The jar I'm consuming that has all my jaxb classes in it also has the package-info class. I tried re-building the component several times and when my consumer pulls it in I see the package-info.class under the jar in the reference libraries. Despite this, my xml output still doesn't have the attribute. I'm fairly new to all this, so is there something else I might be missing? – vogelj Jun 14 '12 at 15:19
  • @vogelj - Is the `package-info` class in the same package as `Report`? Also do you see the same behaviour if you comment of the line where you set the `JAXB_FRAGMENT` property? – bdoughan Jun 14 '12 at 20:28
  • Yes, the `package-info` is in the same package as `Report`. And if I remove the `JAXB_FRAGMENT` property, the above behavior is the same. With the exception of now outputting the xml tag of course. I have always output it as a fragment, which is why the seems completely out of nowhere. – vogelj Jun 14 '12 at 21:08
  • The output I posted above comes from a junit I have. Today I found that a consumer of this jar gets the xml as expected with the version info. But if they run the Junit, it doesn't return the xmlns attribute. This just further confused us on the issue. – vogelj Jun 15 '12 at 22:08
  • Found your conversation in 2021 as I struggle with similar issue. Apparently using PowerMock in junit tests affects this behaviour. Watch out! – Lauri Aug 17 '21 at 09:25