2012-05-28 32 views
0

我將用xml來描述我的問題。如何使用xsd模式和SAX驗證xml?

第一,我定義我的XSD架構與命名空間和它看起來像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://www.example.org/schema" 
xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified"> 

<element name="Clients" type="tns:ClientsType"></element> 


<complexType name="ContactType"> 
    <sequence> 
     <element name="Type" type="string"></element> 
    </sequence> 
    <attribute name="ID" type="long" /> 
</complexType> 


<complexType name="Contact"> 
    <sequence> 
     <element name="ContactType" type="tns:ContactType"></element> 
     <element name="Value" type="string"></element> 
    </sequence> 
    <attribute name="ID" type="long" /> 
</complexType> 

<complexType name="ContactsType"> 
    <sequence> 
     <element name="Contact" type="tns:Contact" maxOccurs="unbounded"></element> 
    </sequence> 

</complexType> 


<complexType name="ClientType"> 
    <sequence> 
     <element name="FirstName" type="string"></element> 
     <element name="SecondName" type="string"></element> 
     <element name="Contacts" type="tns:ContactsType"></element> 
    </sequence> 
    <attribute name="ID" type="long" /> 
</complexType> 


<complexType name="ClientsType"> 
    <sequence> 
     <element name="Client" type="tns:ClientType" maxOccurs="unbounded"></element> 
    </sequence> 
</complexType> 

所以產生明顯的xml看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<tns:Clients xmlns:tns="http://www.example.org/schema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/schema schema.xsd "> 
<tns:Client ID="0"> 
    <tns:FirstName>tns:FirstName</tns:FirstName> 
    <tns:SecondName>tns:SecondName</tns:SecondName> 
    <tns:Contacts> 
     <tns:Contact ID="0"> 
      <tns:ContactType ID="0"> 
       <tns:Type>tns:Type</tns:Type> 
      </tns:ContactType> 
      <tns:Value>tns:Value</tns:Value> 
     </tns:Contact> 
    </tns:Contacts> 
</tns:Client> 

和我的函數用數據創建特定的xml:

  @Override 
     public void printToFile() throws IOException, XMLStreamException { 

     FileWriter fd = new FileWriter(outPath); 
     Random rand = new Random(); 
     int end = rand.nextInt(1000); 
     try{ 
     XMLOutputFactory xof = XMLOutputFactory.newInstance(); 
     XMLStreamWriter xtw = null; 

    xtw = xof.createXMLStreamWriter(fd); 


    xtw.writeStartDocument(); 

    //xtw.setPrefix("tns", "http://www.example.org/schema"); 
     xtw.writeStartElement("Clients"); 
     xtw.writeNamespace("tns", "http://www.example.org/schema"); 

    xtw.writeStartElement("tns","Client", "http://www.example.org/schema"); 
      //xtw.writeStartElement("Client"); 
      xtw.writeAttribute("ID", "1"); 
      xtw.writeStartElement("FirstName"); 
       xtw.writeCharacters("NameFirst"); 
      xtw.writeEndElement(); 
      xtw.writeStartElement("SecondName"); 
       xtw.writeCharacters("NameSecond"); 
      xtw.writeEndElement(); 
      xtw.writeStartElement("Contacts"); 
      for(int i=1;i<2;i++){ 
        xtw.writeStartElement("Contact"); 
         xtw.writeAttribute("ID",Integer.toString(i)); 
         xtw.writeStartElement("ContactType"); 
          xtw.writeAttribute("ID", Integer.toString(i)); 
          xtw.writeStartElement("Type"); 
           xtw.writeCharacters("mojTyp"); 
          xtw.writeEndElement();      
         xtw.writeEndElement(); 
          xtw.writeStartElement("Value"); 
           xtw.writeCharacters("value"); 
          xtw.writeEndElement(); 
        xtw.writeEndElement(); 
      } 

      xtw.writeEndElement(); 


     //xtw.writeEndElement(); 
     xtw.writeEndDocument(); 



    }catch(XMLStreamException xmlE) 
    { 

    } 
    finally 
    { 
     fd.close(); 
    } 

} 

和我生成簡單的JUnit測試usinx薩克斯來驗證我的XML我的函數創建:

@Test 
public void testPrintToFileGoodXml() throws IOException, 
     XMLStreamException, FactoryConfigurationError, SAXException { 
    mService.printToFile(); 
    XMLStreamReader reader = XMLInputFactory.newInstance() 
      .createXMLStreamReader(new FileInputStream(myPath)); 
    SchemaFactory factory = SchemaFactory 
      .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

    Schema schema = factory.newSchema(new File(mySchemaPath)); 
    Validator validator = schema.newValidator(); 

    validator.validate(new StAXSource(reader)); 
    assertTrue(true); 
} 

但測試失敗,所以我覺得這是我的printToFile功能問題的命名空間和連接在xsd模式中定義的其他屬性,我不知道如何解決它。我讀了很多網站和定義xsd的文檔,但我仍然不知道如何解決我的問題。請幫忙。

回答

1

的模式說,根元素應該是客戶

Graphical representation of schema

但是你的XML代碼段有一個開口元素TNS:客戶端,但沒有coresponsing密封元件。檢查你的writeEndElement調用。

<?xml version="1.0" encoding="UTF-8"?> 
<tns:Clients xmlns:tns="http://www.example.org/schema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/schema Contacts.xsd "> 
    <tns:Client ID="0"> 
     <tns:FirstName>tns:FirstName</tns:FirstName> 
     <tns:SecondName>tns:SecondName</tns:SecondName> 
     <tns:Contacts> 
      <tns:Contact ID="0"> 
       <tns:ContactType ID="0"> 
        <tns:Type>tns:Type</tns:Type> 
       </tns:ContactType> 
       <tns:Value>tns:Value</tns:Value> 
      </tns:Contact> 
     </tns:Contacts> 
    </tns:Client> 
</tns:Clients> <--------- Missing 
+0

我檢查了它,並對不起這是我的錯,因爲我的代碼複製,所以缺少一部分xml,所以這不是問題。我認爲問題在於創建xml的函數。正如您在生成的xml中看到的,沒有targetNamespace,我不知道如何正確生成它。當我嘗試xml根本沒有生成,只有它的一部分不包含第一個元素。 – RMachnik