2010-12-09 29 views
0

我正在JAXB 2.0上工作,目前在驗證部分工作不正常。下面 是驗證碼JAXB 2.0驗證不起作用

public void validateXMLToSchema(Unmarshaller ummarshaller,String xsdFileName) throws SAXException, JAXBException{ 
    System.out.println(getClass().getResource(DEFAULT_XSD_NAME).toString()); 
    Schema schema; 
    SchemaFactory schemaFactory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    if(null==xsdFileName) 
     schema=schemaFactory.newSchema(getClass().getResource(DEFAULT_XSD_NAME)); 

    else 
     schema=schemaFactory.newSchema(new File(xsdFileName)); 

    ummarshaller.setSchema(schema); 
    ummarshaller.setEventHandler(new ValidationEventHandler() { 

     @Override 
     public boolean handleEvent(ValidationEvent validationevent) { 
      if(validationevent.getSeverity()==ValidationEvent.FATAL_ERROR || validationevent.getSeverity()==ValidationEvent.ERROR || validationevent.getSeverity()==ValidationEvent.WARNING){ 
       ValidationEventLocator locator = validationevent.getLocator(); 
       log.info("Line:Col[" + locator.getLineNumber() 
         + ":" + locator.getColumnNumber() 
         + "]:" + validationevent.getMessage()); 
      } 
      return true; 
     } 
    }); 

} 

,這裏是在調用方法

Destination destination=new Destination(); 
    try { 
     destination=(Destination)unmarshal(Destination.class,new FileInputStream(new File("C:/Users/Raisonne/Desktop/jaxb/jaxb-ri-20101119/bin/destination.xml"))); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } 
    System.out.println(destination.getName()); 

} 

public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) 
throws JAXBException, SAXException { 
String packageName = docClass.getPackage().getName(); 
JAXBContext jc = JAXBContext.newInstance(packageName); 
Unmarshaller u = jc.createUnmarshaller(); 
XMLValidator xmlValidator=new XMLValidator(); 
xmlValidator.validateXMLToSchema(u, null); 

我有幾個字段爲必填字段按XSD但即使除去它們是人的可持續發展,同時給我的錯誤了不給任何東西,並解析我的XML文件到相應的對象 可以任何一點出現什麼問題?

這裏是XSD

<xs:element name="destination" type="Destination"/> 
    <xs:complexType name="Destination"> 
    <xs:sequence> 
     <xs:element name="name" type="xs:string"/> 
     <xs:element name="destinationID" type="xs:string" minOccurs="0"/> 
     <xs:element name="shortDescription" type="xs:string" minOccurs="0"/> 
     <xs:element name="longDescription" type="xs:string" minOccurs="0"/> 
     <xs:element name="stateID" type="xs:string"/> 
     <xs:element name="typeCode" type="xs:int"/> 
     <xs:element name="countryCode" type="xs:string"/> 
     <xs:element name="categories" type="xs:string"/> 
     <xs:element name="transport" type="Transport" minOccurs="0" maxOccurs="1"/> 
     <xs:element name="culture" type="Culture" minOccurs="0" maxOccurs="1"/> 
     <xs:element name="events" type="Events" minOccurs="0" maxOccurs="1"/> 
     <xs:element name="placesToVisit" type="PlacesToVisit" minOccurs="0" maxOccurs="1"/> 
     <xs:element name="contacts" type="Contact" minOccurs="0" maxOccurs="1"/> 
     <xs:element name="addresses" type="address" minOccurs="0" maxOccurs="1"/> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

的部分和Java文件生成

@XmlElement(required = true) 
protected String name; 
protected String destinationID; 
protected String shortDescription; 
protected String longDescription; 
@XmlElement(required = true) 
protected String stateID; 

在我從XML文件,但仍處於驗證部分沒有報警去除STATEID

感謝提前

+0

提供相應的架構和JAXB對象。 – 2010-12-09 15:03:25

+0

有很多創建對象只需要根對象? – 2010-12-09 15:08:47

回答

1

您的代碼片段是有點難以遵循,是一個解組實際發生?您可能需要在最後一行添加到您的解組方法:

public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) throws JAXBException, SAXException { 
    String packageName = docClass.getPackage().getName(); 
    JAXBContext jc = JAXBContext.newInstance(packageName); 
    Unmarshaller u = jc.createUnmarshaller(); 
    XMLValidator xmlValidator=new XMLValidator(); 
    xmlValidator.validateXMLToSchema(u, null); 
    u.unmarshal(inputStream); 
} 
0

根據這條線

xmlValidator.validateXMLToSchema(u, null); 

您沒有將XSD文件名賦予XmlValidator。它應該像

xmlValidator.validateXMLToSchema(u, "/opt/projects/myschema.xsd"); 

編輯:嗯,這是我怎麼設法做到這一點,它的工作完美:

void validate(MyRequest requestParameters) throws IllegalArgumentException,MalformedURLException,SAXException { 
    try { 
     JAXBContext context = JAXBContext.newInstance(MyRequest.class.getPackage().getName()); 
     Marshaller marshaller = context.createMarshaller(); 
     marshaller.setSchema(getSchema()); 
     JAXBElement<MyRequest> rootElement = new JAXBElement<SearchCustomersRequest>(new QName("http://mysite.com/xsd/myproject/", "MyRequest"), 
         MyRequest.class, requestParameters); 
     marshaller.marshal(rootElement, new DefaultHandler()); 
     log.debug("Validation successful"); 
    } catch (JAXBException e) { 
     throw new IllegalArgumentException("Invalid request parameters: " + e.toString(), e); 
    } 
} 

private Schema getSchema() throws MalformedURLException, SAXException { 
    if (schema == null) { 
     SchemaFactory factory = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); 
     URL schemaUrl = new URL("http://mysite.com/xsd/myproject/schema-1.0.xsd"); 
     schema = factory.newSchema(schemaUrl); 
    } 
    return schema; 
} 
+0

我有我的validateXMLToSchema方法處理它,如果這是null可以看看我的帖子中的代碼 – 2010-12-09 15:38:36

+0

也許是命名空間問題,我解決了創建一個帶有定義的QName的JAXBElement(「http://mysite.com/xsd/ myproject /「..) – 2010-12-09 15:49:12

0

真是奇怪,當我改變了我的代碼,這

public class UnMarshallXML { 

/** 
* @param args 
* @throws SAXException 
*/ 
public static void main(String[] args) throws SAXException { 
    Destination destination=new Destination(); 
    try { 
     destination=(Destination)unmarshal(Destination.class,new FileInputStream(new File("C:/Users/Raisonne/Desktop/jaxb/jaxb-ri-20101119/bin/destination.xml"))); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } 
    System.out.println(destination.getName()); 

} 

public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) 
throws JAXBException, SAXException { 
String packageName = docClass.getPackage().getName(); 
System.out.println(packageName); 
JAXBContext jc = JAXBContext.newInstance(packageName); 
Unmarshaller u = jc.createUnmarshaller(); 
SchemaFactory schemaFactory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
Schema schema=schemaFactory.newSchema(new File("C:/Users/Raisonne/Desktop/jaxb/jaxb-ri-20101119/bin/destination.xsd")); 

u.setSchema(schema); 
u.setEventHandler(
     new ValidationEventHandler() { 
      // allow unmarshalling to continue even if there are errors 
      public boolean handleEvent(ValidationEvent ve) { 
       // ignore warnings 
       if (ve.getSeverity() != ValidationEvent.WARNING) { 
        ValidationEventLocator vel = ve.getLocator(); 
        System.out.println(
          "Line:Col[" + vel.getLineNumber() 
          + ":" + vel.getColumnNumber() 
          + "]:" + ve.getMessage()); 
       } 

       return true; 
      } 
     }); 
// XMLValidator xmlValidator=new XMLValidator(); 
//xmlValidator.validateXMLToSchema(u, null,inputStream); 
@SuppressWarnings("unchecked") 
JAXBElement<T> doc = (JAXBElement<T>)u.unmarshal(inputStream); 
return doc.getValue(); 

}

}

一切開始那樣工作魅力..