On this Page

    _parser.fixed

    Convert fixed-column records.

    Syntax

    _parser.fixed(record, format)

    Parameters

    record is the text to be parsed.

    format is a JavaScript object that describes the structure of the data record. This will typically be a JavaScript constant description such as:

    {first: {from:10, width:30, trim:'right'},
     second: {from:50, width:5, trim:'both'},
     ...}

    As can be seen from the above example each data field (first, second, etc.) has an associated definition of how to extract the requisite data. from defines the data column where the data field begins (first column is number 0). width is the field's column width. trim is an optional setting that indicates whether leading or trailing blanks should be removed automatically:

    left removes leading blanks.
    right removes trailing blanks.
    both removes leading and trailing blanks.
    none does not remove any blanks. This is the default setting.

    Returned Value

    The returned value of this function is a JavaScript object whose properties are the names from the names parameter and values are the associated values from the data record. If the record is incorrectly formatted and cannot be parsed, the return value is null.

    Description

    This routine is used to parse a single fixed-format record and assign names to all the values in that record.

    Example

    The following example is similar to what the _parser.fixedToXml function will do: convert an entire fixed-format file to XML. However, unlike the _parser.fixedToXml function, this code creates a single document structure with each record from the file being enclosed in <DetailLine>..</DetailLine> tags.

    Consider a file C:/DocOrigin/test/Fixed.txt that contains fix-format records. Here's a sample of some of those records:

               1         2         3         4         5         6         7         8         9         
    0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
               1      AK036    Happy Cat Activity Centre BlEach          23.12   0.00       23.12
               0      SC180    S/P Swing N Play Cat Toy*DELEach           1.10   7.00        0.00
               3      AH589    Atlas 10 Pet Carry Entry LvlEach          22.00   7.00       20.46
               4      AH590    Atlas 20 Pet Carry Entry LvlEach          27.50   7.00       25.58
               2      VP611    VP Super All Wormer Cat 4 TaEach           9.25   7.00        8.60
               ...

    The following code will read all records in a file and convert it to an equivalent XML file.

    var sFormat = { Quantity:{from:6, width:6, trim:'both'},
     			Code:{from:16, width:7, trim:'both'},
     			Description:{from:27, width:28, trim:'both'},
     			Price:{from:64, width:10, trim:'both'},
     			Discount:{from:74, width:10, trim:'both'},
     			Charged:{from:84, width:10, trim:'both'},
     			Net:{from:94, width:12, trim:'both'}  };
    
    var fp = _file.fopen("C:/DocOrigin/test/Fixed.txt", "r");	// Open the data file
    if (!fp) _message("File open error");
    
    var xml = new XmlFile("C:/DocOrigin/test/Fixed.xml");		// Open the xml output file
    
    var sRec;
    var obj;
    
    while (sRec = fp.fgets()) { 			// read each record
    	if (sRec.length < 5)continue;
    
    	obj = _parser.fixed(sRec, sFormat);	// converts record
    	if (obj) {
     		xml.DetailLine = obj;			// format and write as xml
    	}
    }
    
    fp.fclose();
    xml.close();

    This will produce a new file C:/DocOrigin/test/Fixed.xml that looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <XMLData>
    <Document>
      <DetailLine>
        <Quantity>1</Quantity>
        <Code>AK036</Code>
        <Description>Happy Cat Activity Centre Bl</Description>
        <Price>23.12</Price>
        <Discount>0.00</Discount>
        <Charged>23.12</Charged>
        <Net>23.12</Net>
      </DetailLine>
      <DetailLine>
        <Quantity>0</Quantity>
        <Code>SC180</Code>
        <Description>S/P Swing N Play Cat Toy*DEL</Description>
        <Price>1.10</Price>
        <Discount>7.00</Discount>
        <Charged>0.00</Charged>
        <Net>0.00</Net>
      </DetailLine>
      <DetailLine>
        <Quantity>3</Quantity>
        <Code>AH589</Code>
        <Description>Atlas 10 Pet Carry Entry Lvl</Description>
        <Price>22.00</Price>
        <Discount>7.00</Discount>
        <Charged>20.46</Charged>
        <Net>61.38</Net>
      </DetailLine>
      <DetailLine>
        <Quantity>4</Quantity>
        <Code>AH590</Code>
        <Description>Atlas 20 Pet Carry Entry Lvl</Description>
        <Price>27.50</Price>
        <Discount>7.00</Discount>
        <Charged>25.58</Charged>
        <Net>102.30</Net>
      </DetailLine>
      <DetailLine>
        <Quantity>2</Quantity>
        <Code>VP611</Code>
        <Description>VP Super All Wormer Cat 4 Ta</Description>
        <Price>9.25</Price>
        <Discount>7.00</Discount>
        <Charged>8.60</Charged>
        <Net>17.20</Net>
      </DetailLine>
      ...
    </Document>
    </XMLData>

    See Also

    _parser.delimited
    _parser.delimitedToXml
    _parser.fixedToXml
    _parser.parms