Question

I'm using the below code to unmarshal an XML to a set of Java classes (classes generated from an xsd).

JAXBContext jc = JAXBContext.newInstance(BlockType.class);
StreamSource xml = new StreamSource("C:\\Users\\Se\\Desktop\\watch2.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<BlockType> je1 = unmarshaller.unmarshal(xml, BlockType.class);

BlockType cgwt = je1.getValue();

The XML that has to be unmarshalled has at its beginning a line like this one:

<test:MessageBlock xsi:schemaLocation="http://www.test.com/Watch/v2.1 Test_WatchSchema_v2.1.xsd">

If I remove the test: and xsi:.... the XML gets unmarshalled correctly. However, if I let them I get

[org.xml.sax.SAXParseException: The prefix "test" for element "test:CargoWatchMessageBlock" is not bound.]

How can I fix this ?

Was it helpful?

Solution

There should be an xmlns:test attribute in your XML document if there is an test prefix used. Without that attribute declaring the prefix you should remove the prefix from your document for it to be valid.


UPDATE

I do not control the document so I guess I'll have to find some workaround for removing the test: prefix and xsi: before unmarshalling.

Below is a link to an example that should help you build what you are looking for. It leverages JAXB's UnmarshallerHandler with a SAX parser to treat the prefix as parent of the element name.

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