Skip to main content

UUID.toString() is not a valid NCName...

4 replies [Last post]
tazman
Offline
Joined: 2009-08-12
Points: 0

When trying to validate a XML file with a schema while unmarshalling I came across this problem.

I figured out that an NCName has to start with a letter or an underscore. But of course UUIDs might start with figures as well. So is there a better way as to use UUID as XmlRefIds? Or can I somehow work with TypeAdapters without loosing the reference while unmarshalling.

This is the whole error stacktrace:

javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '6b74c9e9-2156-4712-b0ea-fd880d2a7260' is not a valid value for 'NCName'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at .. .jpa.jaxb_ersteSchritte.JAXB1teSchritte.main(JAXB1teSchritte.java:77)
Caused by: org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '6b74c9e9-2156-4712-b0ea-fd880d2a7260' is not a valid value for 'NCName'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3068)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:2978)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2121)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:791)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:563)
at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:94)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:156)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
... 4 more

Long question short:

Why isn´t it allowed to use UUIDs for @XmlIds? And is there any way to make it work?

If you (don´t) know the answer to this, maybe you could help me out here http://forums.java.net/jive/thread.jspa?threadID=65576&tstart=0 ;)

Thanks,
Tazman

Message was edited by: tazman

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tazman
Offline
Joined: 2009-08-12
Points: 0

Every thing works as it should... execpt my RandomFillIn.getFilledClass();

Thanks for reading, and have fun! ;)

Bye,
Tazman

tazman
Offline
Joined: 2009-08-12
Points: 0

Solution I thougt I found, wasn´t one...

tazman
Offline
Joined: 2009-08-12
Points: 0

I found the solution, I think.

No, i didn´t...

Message was edited by: tazman

tazman
Offline
Joined: 2009-08-12
Points: 0

I dug a little bit deeper...

I tried to convert my UUIDs into valid NCNames with the following annotation

____
@XmlJavaTypeAdapter(value = XmlUuidAdapter.class)
@XmlID
private String uuid;
____

and this adapter:
____
package .. .jpa.jaxb_ersteSchritte;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class XmlUuidAdapter extends XmlAdapter{

@Override
public String marshal(String v) throws Exception {
String ret = "uuid_" + v;
return ret;
}

@Override
public String unmarshal(String v) throws Exception {
String ret = v.substring(5);
return ret;
}

}
____

But now I´m getting this interesting error:

____
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-id.1: There is no ID/IDREF binding for IDREF 'uuid_398e0270-c8a0-4eb8-8ab6-b807e4d8b11d'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at .. .jpa.jaxb_ersteSchritte.JAXB1teSchritte.main(JAXB1teSchritte.java:77)
Caused by: org.xml.sax.SAXParseException: cvc-id.1: There is no ID/IDREF binding for IDREF 'uuid_398e0270-c8a0-4eb8-8ab6-b807e4d8b11d'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2191)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:791)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:563)
at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:94)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:156)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
... 4 more
____

Compared to the error I got before now the ref is lost. So it seems, that "my" UUID is converted back into a real UUID and then the search for the ref starts before the referenced UUID is converted?!

Another interesting thing is that when I outcomment the schema validation the whole thing runs smoothly.

So the validation seems to has influence on the order of actions?! Without validation either every ref is checked and after that the UUIDs are converted (or the other way around) und with validation these steps got mixed up...
____

public class JAXB1teSchritte {

public static void main(String[] args){

try{
JAXBContext context = JAXBContext.newInstance(EntityEnvelope.class);

Marshaller marshaller = context.createMarshaller();
Unmarshaller unmarshaller = context.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

EntityEnvelope umschlag = new EntityEnvelope();

Person testPerson = (Person) RandomFillIn.getFilledClass(Person.class);
Group testGruppe = (Group) RandomFillIn.getFilledClass(Group.class);
CashBox testCashbox = (CashBox) RandomFillIn.getFilledClass(CashBox.class);
Person testPerson1 = (Person) RandomFillIn.getFilledClass(Person.class);
Person testPerson2 = (Person) RandomFillIn.getFilledClass(Person.class);
Person testPerson3 = (Person) RandomFillIn.getFilledClass(Person.class);
Group testGruppe1 = (Group) RandomFillIn.getFilledClass(Group.class);

testPerson.setGrp(testGruppe);
testGruppe.setCashbox(testCashbox);
testGruppe.setLeader(testPerson);
testGruppe.getMembers().add(testPerson1);
testGruppe.getMembers().add(testPerson2);
testGruppe.getMembers().add(testPerson3);
testPerson1.setGrp(testGruppe1);
testPerson2.setGrp(testGruppe1);
testPerson3.setGrp(testGruppe1);
testGruppe.setLeader(testPerson);

List

umschlagListe = umschlag.getEntityList(); umschlagListe.add(testPerson); umschlagListe.add(testPerson1); umschlagListe.add(testPerson2); umschlagListe.add(testPerson3); umschlagListe.add(testGruppe); umschlagListe.add(testGruppe1); umschlagListe.add(testCashbox); FileWriter fw = new FileWriter( "Umschlag.xml" ); marshaller.marshal(umschlag, fw); fw.close(); Schema schema = sf.newSchema(new File("src/main/java/schema1.xsd")); --> //unmarshaller.setSchema(schema); <-- FileReader fr = new FileReader("Umschlag.xml"); EntityEnvelope unmarshalledUmschlag = (EntityEnvelope) unmarshaller.unmarshal(fr); System.out.println(unmarshalledUmschlag.getEntityList().size()); }catch (Exception e){ e.printStackTrace(); } } } ___ I´m pretty much lost here... Thanks, Tazman