Menu

Readline a file

Help
Anuj
2012-01-17
2012-09-04
  • Anuj

    Anuj - 2012-01-17

    Hi,

    I want to read file line by line and add each line in list.

     
  • Jason Bassett

    Jason Bassett - 2012-01-17

    Hello

    Have the same requirement myself, no luck yet, report back if you find the
    ways and means,

    Regards

    Jason

     
  • Steven P. Goldsmith

    Why not use BeanShell? I'm actually parsing a site and storing data in a list.
    I have a common.cml I use to define common variables (note you must use 2.1
    trunk version of WH):

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- 
    Common variables and functions 
    -->
    
    <config xmlns="[url]http://web-harvest.sourceforge.net/schema/2.1/core[/url]" charset="UTF-8">
    
        <script>
            <![CDATA[
                // HTTP client defaults
                http.client.params.soTimeout = 5000;
                http.client.params.connectionManagerTimeout = 5000;
                http.client.httpConnectionManager.params.connectionTimeout = 5000;
                // Default user agent
                String userAgent = "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
                // List of Maps to be returned to Java application
                List mapList = new ArrayList();
            ]]>
        </script>
    
    </config>
    

    Then for example create a couple functions to parse XHTML and add to a List of
    Maps

         <!--
        Return list of Strings parsing each element of xmlDoc.
         -->
    
        <function name="getList">
            <return>
                <loop item="item" index="i">
                    <list>
                        <!-- Get a list of TR elements of name DataContainer using
                        URL postUrl -->
                        <xpath expression='//tr[@name="DataContainer"]'>
                            <value-of expr="${xmlDoc}"/>
                        </xpath>
                    </list>
                    <body>
                        <xpath expression="//span[@name='INCIDENT']/text()">
                            <value-of expr="${item}"/>
                        </xpath>
                        <xpath expression="(//span[@name='DATE_TIME']/text(), ' ')[1]">
                            <value-of expr="${item}"/>
                        </xpath>
                        <xpath expression="(//span[@name='eTIME']/text(), ' ')[1]">
                            <value-of expr="${item}"/>
                        </xpath>
                        <xpath expression="(//span[@name='DESCRIPTION']/text(), ' ')[1]">
                            <value-of expr="${item}"/>
                        </xpath>
                        <xpath expression="(//span[@name='STREET']/text(), ' ')[1]">
                            <value-of expr="${item}"/>
                        </xpath>
                        <xpath expression="(//span[@name='SUBDIVISION']/text(), ' ')[1]">
                            <value-of expr="${item}"/>
                        </xpath>
                    </body>
                </loop>
            </return>
        </function>
    
        <!--
        Process calls List and store fields in Map, then add to List.
        -->
    
        <function name="addToMapList">
            <script>
            <![CDATA[
           // Date format
           simpleDateFormat = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm");
           List list = calls.toList();
           Iterator listIterator = list.iterator();
           Map map = null;
           String tempStr = null;
           while (listIterator.hasNext()){
               map = new HashMap();
               // ID for MCSO
               map.put("entity_id", 1);
               // Event number
               map.put("event_num", listIterator.next().toString());
               // Timestamp as java.util.Date which must be converted to java.sql.Timestamp for database
               map.put("event_time", simpleDateFormat.parse(listIterator.next().toString()+" "+listIterator.next().toString()));
               // Description
               tempStr = listIterator.next().toString();
               if (tempStr.toString().trim().equals("")) {
                  map.put("description", null);
               } else {
                   map.put("description", tempStr);
               }
               // Main location
               tempStr = listIterator.next().toString();
               if (tempStr.toString().trim().equals("")) {
                  map.put("location_main", null);
               } else {
                   map.put("location_main", tempStr);
               }
               // Alternate location 1
               tempStr = listIterator.next().toString();
               if (tempStr.toString().trim().equals("")) {
                  map.put("location_alt1", null);
               } else {
                   map.put("location_alt1", tempStr);
               }           
               // These don't exist, so set to null
               map.put("case_num", null);
               map.put("location_alt2", null);
               map.put("location_alt3", null);
               mapList.add(map);
            }        
            ]]>
            </script>      
        </function>
    
     

Log in to post a comment.