2013-07-30 115 views
0

如何驗證xml文件是否與字符串形式的multischema xsd相對應。 我只有在運行時從數據庫檢索到我的xsd文件,而且我不想在文件系統上創建任何文件。所以我必須針對xsd字符串驗證xml。驗證xml是否對xsd字符串

感謝

更新: 我的實際問題是import語句和包括將要文件鏈接報表。我想知道如何處理導入幷包含指向數據庫中字符串的語句。我的意思是我不能在任何時候創建一個文件。它應該是一個字符串。

+0

哪種語言? –

+0

無論你認爲能做到這一點。 Java,python ...任何語言。 – user1900588

+0

大多數語言都可以做到。答案取決於你使用哪種語言 –

回答

0

嗨,我做了這個小例子。

我認爲它可以進行優化,但它爲您提供瞭解決方案的全球理念。 :-)

import java.io.ByteArrayInputStream; 
import java.io.InputStream; 

import javax.xml.transform.Source; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.SchemaFactory; 

import org.xml.sax.SAXException; 

public class xsd { 


    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     String xsd = "Your XSD"; 
     String xsd2 = "2nd XSD"; 

     InputStream is = new ByteArrayInputStream(xsd.getBytes()); 
     InputStream is2 = new ByteArrayInputStream(xsd2.getBytes()); 

     SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); 

     Source source = new StreamSource(is); 
     Source source2 = new StreamSource(is2); 

     try { 
      sf.newSchema(new Source[]{source,source2}); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

對於從字符串到InputStream的轉換我使用這個帖子:http://stackoverflow.com/questions/247161/how-do-i-turn-a-string-into-a-stream-in-java – sgirardin

+0

謝謝你的響應。請檢查我的帖子更新。我可以輕鬆地將文件讀取爲字符串,但我需要的是導入語句和包含語句。如何映射導入幷包含到字符串而不是文件鏈接。 – user1900588