2012-08-16 92 views
2

Web上有許多關於此驗證過程的手冊。儘管如此,我無法找到我的代碼無法正常工作的原因。輸入模式和XML的值我得到here使用XMLReader驗證XML與XSD

static String schemaString ="<?xml version=\"1.0\"?>" + 
     "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + 
     " targetNamespace=\"http://www.java2s.com\"" + 
     " xmlns=\"http://www.java2s.com\"" + 
     " elementFormDefault=\"qualified\">" + 
     "<xs:element name=\"note\">" + 
     "<xs:complexType>" + 
     "<xs:sequence>" + 
     "<xs:element name=\"to\" type=\"xs:string\"/>" + 
     "<xs:element name=\"from\" type=\"xs:string\"/>" + 
     "<xs:element name=\"heading\" type=\"xs:string\"/>" + 
     "<xs:element name=\"body\" type=\"xs:string\"/>" + 
     "</xs:sequence>" + 
     "</xs:complexType>" + 
     "</xs:element>" + 
     "</xs:schema>"; 

static String xmlString = "<?xml version=\"1.0\"?>" + 
     "<note>" + 
     "<to>rtoName</to>" + 
     "<from>FromName</from>" + 
     "<heading>Info</heading>" + 
     "<body>Message Body</body>" + 
     "</note>"; 

    String xml; 
    XMLReader xmlReader = null; 
    try { 

     SAXSource source = new SAXSource(new InputSource(new StringReader(schemaString))); 
     SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
     Schema schema = schemaFactory.newSchema(source); 

     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     spf.setSchema(schema); 
     SAXParser saxParser = spf.newSAXParser(); 
     xmlReader = saxParser.getXMLReader(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // parsing step after all preconditions succeeded 
    try { 
      xmlReader.parse(new InputSource(new StringReader(xmlString))); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

執行結果是這樣的:

[Error] :1:28: cvc-elt.1: Cannot find the declaration of element 'note'. 

在調試模式似乎一切都很好。架構設置正確,依此類推。想法?

+1

可能是它的命名空間的問題你的架構有目標命名空間,所以你可以像trysomething <注的xmlns =「http://www.java2s.com」> – 2012-08-16 15:28:08

+0

@ KonstantinV.Salikhov沒有變化,但您的建議適用於Validator使用。 – Steve 2012-08-16 16:44:12

回答

2

我想問題是您的XML中的未定義名稱空間。

嘗試明確設置命名空間(看你在targetNamespace XSD:

static String xmlString = "<?xml version=\"1.0\"?>" + 
    "<note xmlns=\"http://www.java2s.com\">" + 
    "<to>rtoName</to>" + 
    "<from>FromName</from>" + 
    "<heading>Info</heading>" + 
    "<body>Message Body</body>" + 
    "</note>"; 
+0

不,它不會工作。還是一樣的錯誤。我很好奇原因。 – Steve 2012-08-16 16:43:26

+0

我找到了原因,並將其寫入這裏爲未來世代:)它必須手動命名空間感知SAXParserFactory方法 - spf.setNamespaceAware(true)。我認爲這個過程已經在Validator API裏完成了,因此 - 不需要。謝謝你們的幫助。 – Steve 2012-08-16 17:11:46

2

有爲什麼要使用XMLReader
我只是想呈現另一種方式特殊原因,你可能會發現它有用?

import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator;  

// ... 

    try { 
     // load schema from file 
     File schemaFile = new File(schemaLocation); 
     // load xml source form string holding the content 
     Source xmlFile = new StreamSource(new StringReader(fileContent)); 

     SchemaFactory schemaFactory = SchemaFactory 
      .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

     Schema schema = schemaFactory.newSchema(schemaFile); 

     Validator validator = schema.newValidator(); 

     validator.validate(xmlFile); 

     System.out.println("XML is valid"); 

    } catch (Exception e) { 

     System.out.println("XML is NOT valid"); 
     System.out.println("Reason: " + e.getMessage()); 

    } 
+0

感謝您的回答,我試過了,但它不起作用。但是,康斯坦丁和DRCB的建議終於奏效了! – Steve 2012-08-16 16:42:36

+0

這種方式適合我... – rogerdpack 2013-03-19 18:24:58