Problem z JAXB

0

Hej,

Mam bardzo dziwny problem z parserem JAXB. Chcę sparsować plik XML który w głównym elemencie nie ma opisu przestrzeni nazw np:

<root name="name">
    <value name="nam">vlaue<value>
    <value name="nam">vlaue<value>
</root>

Więc dopisałem sobie filter do przestrzeni nazw:

public class NamespaceFilter extends XMLFilterImpl {

	private String usedNamespaceUri;

	private boolean addNamespace;

	private boolean addedNamespace = false;

	public NamespaceFilter(String namespaceUri, boolean addNamespace) {
		super();

		if (addNamespace)
			this.usedNamespaceUri = namespaceUri;
		else
			this.usedNamespaceUri = "";

		this.addNamespace = addNamespace;
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();

		if (addNamespace) {
			startControlledPrefixMapping();
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {

		super.startElement(this.usedNamespaceUri, localName, qName, atts);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		super.endElement(this.usedNamespaceUri, localName, qName);
	}

	@Override
	public void startPrefixMapping(String prefix, String url)
			throws SAXException {

		if (addNamespace) {
			this.startControlledPrefixMapping();
		}
	}

	private void startControlledPrefixMapping() throws SAXException {

		if (this.addNamespace && !this.addedNamespace) {

			super.startPrefixMapping("", this.usedNamespaceUri);
			this.addedNamespace = true;
		}
	}
}

I teraz najciekawsze jak parsuje w następujący sposób:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(config.getResponseSchema()) });

NamespaceFilter nsFiler = new NamespaceFilter("http://www.kaziuuu.com/schema",true);

JAXBContext jaxbContext = JAXBContext.newInstance(config.getResponseContextPackage());
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);

InputSource inputSrc = new InputSource(input);
inputSrc.setEncoding(config.getEncoding());
SAXSource saxSource = new SAXSource(nsFiler, inputSrc);
JAXBElement xmlElement = xmlElement = (JAXBElement) jaxbUnmarshaller.unmarshal(saxSource);

To jest ok, ale jak już zamiast stringu "http://www.kaziuuu.com/schema" użyje zmiennej np:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(config.getResponseSchema()) });

String nameSpace = new String("http://www.kaziuuu.com/schema");
NamespaceFilter nsFiler = new NamespaceFilter(nameSpace,true);

JAXBContext jaxbContext = JAXBContext.newInstance(config.getResponseContextPackage());
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);

InputSource inputSrc = new InputSource(input);
inputSrc.setEncoding(config.getEncoding());
SAXSource saxSource = new SAXSource(nsFiler, inputSrc);
JAXBElement xmlElement = xmlElement = (JAXBElement) jaxbUnmarshaller.unmarshal(saxSource);

To dostaje błąd:
javax.xml.bind.UnmarshalException: Namespace URIs and local names to the unmarshaller needs to be interned.
Wie ktoś o co może chodzić :|

Pozdrawiam,
kaziuuu

0

A jak zmienna zdefiniojesz tak:
String nameSpace = "http://www.kaziuuu.com/schema";
zamiast
String nameSpace = new String("http://www.kaziuuu.com/schema");
?
Dziala?

0

Wyjatek mowi ze "must be interned". Zrob:
String nameSpace = new String("http://www.kaziuuu.com/schema");
nameSpace = nameSpace.intern();

Sposob ktory podalem poprzednio rowniez powinien dzialac. Wynika z tego ze te stringi gdziestam gleboko sa porownywane za pomoca == a nie equals, ciekawe dlaczego?

0

Dzięki za odpowiedź rzeczywiście jak dam intern() to działa :-) Tylko właśnie po co oni takie dziwactwo zrobili...

1 użytkowników online, w tym zalogowanych: 0, gości: 1