NULL出现在FLAT文件中,无法使用XSD进行解析

时间:2014-01-21 14:06:07

标签: xml xsd flat-file

我们正在尝试将基于位置的FLAT文件(http://i.stack.imgur.com/EryDU.jpg)转换为有效的XML。该文件包含标题,详细信息行,预告片。

细线有数据字符,而空格字符和其他一些特殊字符如NULL。

填充一个位置长度的NULL字符。空间也填充一个位置长度。

我们使用以下XSD来解析此FLAT FILE

                <?xml version="1.0" encoding="UTF-8" ?>

            <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:nxsd="http://xmlns.oracle.com/pcbpel/nxsd"
                        xmlns:tns="http://xmlns.oracle.com/pcbpel/nxsdABO"
                        targetNamespace="http://xmlns.oracle.com/pcbpel/nxsdABO"
                        elementFormDefault="qualified"
                        attributeFormDefault="unqualified"

                        nxsd:version="NXSD"
                        nxsd:stream="chars"
                        nxsd:encoding="ISO-8859-1"
            >


              <xsd:element name="ROOT">
                <xsd:complexType>
                <xsd:sequence minOccurs="1">
                    <xsd:element name="Header" nxsd:startsWith="H" minOccurs="1" maxOccurs="1">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="InterfaceID" type="xsd:string" nxsd:style="fixedLength" nxsd:length="3" />
                          <xsd:element name="FileSeqNo" type="xsd:int" nxsd:style="fixedLength" nxsd:length="5" />
                          <xsd:element name="TimeStamp" type="xsd:int" nxsd:style="fixedLength" nxsd:length="14" />
                          <xsd:element name="FromSystem" type="xsd:string" nxsd:style="fixedLength" nxsd:length="10" />
                          <xsd:element name="ToSystem" type="xsd:string" nxsd:style="fixedLength" nxsd:length="10" minOccurs="0"/>
                          <xsd:element name="FromSAPSys" type="xsd:string" nxsd:style="fixedLength" nxsd:length="8" minOccurs="0"/>
                          <xsd:element name="FromSAPclt" type="xsd:string" nxsd:style="fixedLength" nxsd:length="3" minOccurs="0"/>
                          <xsd:element name="ToSAPSys" type="xsd:string" nxsd:style="fixedLength" nxsd:length="8" minOccurs="0"/>
                          <xsd:element name="ToSAPclt" type="xsd:string" nxsd:style="fixedLength" nxsd:length="4" minOccurs="0"/>
                          <xsd:element name="UserID" type="xsd:string" nxsd:style="terminated" nxsd:terminatedBy="${eol}" minOccurs="0"/>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>      

                    <xsd:element name="DataLine2" nxsd:startsWith="D012" maxOccurs="unbounded" minOccurs="1">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="TransactionType" type="xsd:string" nxsd:style="fixedLength" nxsd:length="2" minOccurs="0"/>
                          <xsd:element name="Name" type="xsd:string" nxsd:style="fixedLength" nxsd:length="30" />
                          <xsd:element name="PostingKey" type="xsd:string" nxsd:style="fixedLength" nxsd:length="11" />
                          <xsd:element name="AdderssKey" type="xsd:string" nxsd:style="fixedLength" nxsd:length="26" />
                          <xsd:element name="TransactionKey" type="xsd:byte" nxsd:style="terminated" nxsd:terminatedBy="${eol}" minOccurs="0"/>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>


                    <xsd:element name="Trailer" nxsd:startsWith="T" minOccurs="1" maxOccurs="1">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="FRecords" type="xsd:string" nxsd:style="fixedLength" nxsd:length="9" minOccurs="0"/>
                          <xsd:element name="DRecords" type="xsd:string" nxsd:style="terminated"  nxsd:terminatedBy="${eol}" minOccurs="0"/>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                </xsd:sequence>
                </xsd:complexType>
              </xsd:element>

            </xsd:schema>

SOA Suite 11g文件适配器无法解析FLAT文件,当文件具有NULL字符时它返回空XML文档

但是当我们用whi替换那些NULL时,情况正常。

我们是否遗漏了XSD中用于解析NULL的任何内容?

还有其他方法可以处理NULL吗?

需要有人的指导

2 个答案:

答案 0 :(得分:0)

如果用NULL表示带有代码点0的Unicode字符(通常写为NUL),则XML中不允许使用此字符,您必须将其转换为其他字符。

答案 1 :(得分:0)

哦,我们得到了处理它的出路,

我们已经创建了Custom Java来用NITE替换NUL,

            String fileName =(String)getVariableData("FileName"); 
            String fileLocation =(String)getVariableData("FileLocation"); 

            String inputFile = fileLocation + "/" + fileName;
            String outputFile = fileLocation + "/" + fileName + ".temp";

            FileInputStream fileInStream = new FileInputStream(inputFile); 
            DataInputStream inputStream = new DataInputStream(fileInStream); 
            BufferedReader bufferRead =  new BufferedReader(new InputStreamReader(inputStream)); 

            String stringLine = ""; 

            FileWriter fileOutStream = new FileWriter(outputFile); 
            BufferedWriter bufferWrite = new BufferedWriter(fileOutStream); 

                while ((stringLine = bufferRead.readLine()) != null) { 
                    stringLine = stringLine.replaceAll("\0", " "); 
                    bufferWrite.write(stringLine); 
                    bufferWrite.newLine(); 
                } 

            bufferWrite.close(); 

在上面的代码中

stringLine = stringLine.replaceAll("\0", " "); 

将用witespace替换NUL char