0

我将描述我的 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();
    }

}

我使用inx Sax生成简单的Junit测试来验证我的函数创建的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 的文档,但我仍然不知道如何解决我的问题。请帮忙。

4

1 回答 1

1

架构说根元素应该是 Clients

模式的图形表示

但是,您的 XML 片段有一个开始元素 tns:Clients,但没有对应的结束元素。检查您的 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
于 2012-05-29T10:19:01.417 回答