Ryan Knight, �呒������乒���, ����职业��
��: �品������?����员��哟�统业�����芙�������暮芏����裕������魏慰���
探�����使�� Zoho Reports��Zoho API �� XML �������品������������佣�����
��应�贸��颍ú��������械模����潜��氐模���臃������堋�

�品����供一��业������ (BI) �������,���安装��维����统��������时����目�
��统杀�������也�鼙���泛����������剩���为������维�����遣���要专�偶��堋���
��统�谋������不同�������品�������菘���驻���诒�����������暮芏�位���小�
������写��
API��应�贸����探涌�
HTML�����谋��������
HTTP�����谋�����协��
JDOM��Java �牡�����模��
PDF����携式�牡���式
RSS�����������
SQL���构����询����
XML������展�������
URI��统一��源��识��
URL��统一��源��位��
��么���品�������要��战����要��证����芄���效�卮��到���������小��芏��品���平
台����拥��颖��蚨汉欧指�值 (CSV) �募�������荨�����也通�� Web �����供一
����探涌凇�
Zoho® Reports��一��示��应�贸�����统��应�贸������品�����屑��桑�����
�����供�������渭� �慰����希��������械�����展示�舜�统应�贸�����谓� Zoho 
Reports ������存储�图����约�����目�模������越������图��嵌���� Web 页��
�小�
Zoho Reports �供一��������库(���存储��荩���一�� REST-ful API����
��������莸慕�������REST-���募芄�������慕����������为������通�� HTTP 
���屑虻サ���息���荨����眉虻サ� HTTP ��息��������������通�牛����������苑�
�� XML ��式����荨�
�私� Zoho Reports
Zoho Reports ��一�����弑��工�撸���为业������供一�������慕�����������
����悉����库方式�储����织为�����式�����诙�����结构�慕涌��堑��颖������
�菘�幕�稀���也����通�����氐��募��构�����结构��
�����很多方式��������� Zoho Reports �校������������� CSV��Microsoft® 
Office Excel® (XLS) �� HTML �雀�式���兀��诘��颖��之��慕������止���
�;����通��使�� Reports API ���。���诒��功�埽�Zoho Reports �供����
��统����应�贸����锌����业�����些��� �� 图�?透��图�����荼����图��������
通���戏沤��������莸目��臃�����也����使����悉�� SQL ��菘����圆�询��荨�
�� Zoho Reports �卸��报��之�螅��越�����嵌���� Internet �系��魏蔚胤���
���博�汀�����应�贸�������� Web 站�。����涂��������诓�同��位��使����
同�谋��。
��页��
使�� Zoho API
Zoho API 为通�� HTTP ����菘��械���萁����供一�� REST-���慕涌凇��� 
API �牡��冒�����菘����要使�玫谋?�� API �墓��馨�����
��踊��薷���荨�
使�� CSV �募�������踊������荨�
����指���������������禄�删����荨�
������ PDF��HTML��CSV��JavaScript Object Notation (JSON) ��图��之��
�母�式������莺捅��。
使�� SQL SELECT ��询����菘�取����荨�
要使�酶� API���������却� http://writer.zoho.com/apikey.htm ����一����
钥������ API ���枚�要���荽���钥��
每�� Zoho Reports ����牡诙�����要������ ticket �����票�荩�ticket��
�������状蔚�录时��傻摹����票��然���������泻��� API ���谩�
�����式使��一�� HTTP POST �����式�����诓�����指��要��取�亩������约� API 
��钥��票�荨�API �本�������式�������������������写��荨�Zoho wiki ������
���式����细��息���渭� �慰����希���
������指������应��式������ XML �� JSON��XML ��式指��一�� <response> ��
�牵�����嵌����一�� <result> ��恰�<response> ���指�����玫� URI �筒�取
�亩�����

<?xml version="1.0" encoding="UTF-8" ?> 
<response uri="/api/loginname/databasename/tablename" 
action="speci&#64257;edaction">
<result> 
<!-- The specific XML response based on the action goes here --
>
</result> 
</response> 

为�舜俳��� Zoho API �慕�����Zoho Reports �供一���� Java&#8482; ���院� 
Python ���员�写�目突��库;���,Google App Engine �突��吮���为 Python 
���一����展�供�������械�����使�� Java ���库,�每�为�� Zoho Reports ��
�����供一���当�虻サ慕涌凇�
使�玫������� ReportClient�������� Zoho Reports ���������突��恕���始��
��录���突��耍�然���媒������������������ URI��

             ReportClient reportClient = 
ZohoConfig.getReportClient();
        reportClient.login(ZohoConfig.LOGINNAME, 
ZohoConfig.PASSWORD);

        String uri = reportClient.getURI(ZohoConfig.LOGINNAME,
            ZohoConfig.DATABASENAME, "WordFrequency");

�舜����录�� ZohoConfig ��指������库,�� BlogAnalyzer��然��指���私�要
使�玫谋� �� �������� WordFrequency��
�����报��突���之��使�����碌����顺��突��耍�

reportClient.logout();

��页��
���� Zoho Reports ���� RSS ��要
��为一���虻サ����樱������������芏� RSS ��要���页�每�����食��值�频�省����� 
RSS ��使�� XML 指���模����院������� Java �����卸�取�徒��� RSS��示��应�贸�
��使�� Rome���渭� �慰����希���一�����诮�������珊头��� RSS ��要�� Java 
�。Rome 使�� JDOM XML ��������支����要�������约�����转����同��式����要��
���� Rome ��取 RSS ��要��为�虻ァ����要��取�� RSS ��要�� URL������一�� 
XMLReader��然���取��要��

        URL feedUrl = new URL(url);
        XmlReader xmlReader = new XmlReader(feedUrl);
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(xmlReader);

�么��将一系����目�储�� feed �����小�然������缘��通����些��目������目��
������一系�械��剩��� �单 1 ��示��

�单 1. ���通�������目
                

        List entriesList = feed.getEntries();
        for (Object obj : entriesList) {
            SyndEntryImpl syndEntry = (SyndEntryImpl) obj;
            String title = syndEntry.getTitle();
            parseWordsFromString(title);
            SyndContent description = 
syndEntry.getDescription();
            parseWordsFromString(description.getValue());
        }

parseWordsFromString() �������碌���映�,����统����每�����实�频�省�����
��取�玫�每����要��������目之��循��通����钥�����看每�����实�频�省����,��
�� and ������ 1015 �危�Apple ���� 45 �危�Adobe �������巍�
���址��������
�单 2 ����示���诖��址�������实姆����谴� Java2s Web 站���系���同������
�亩����模��渭� �慰����希���

�单 2. ���址��������
                

 public void parseWordsFromString(String nxtString) throws 
CharacterCodingException {

        // Create line pattern
        Pattern linePattern = Pattern.compile(".*$", 
Pattern.MULTILINE);

        // Create word pattern
        Pattern wordBreakPattern = 
Pattern.compile("[\\p{Punct}\\s}]");

        // Match line pattern to buffer
        Matcher lineMatcher = linePattern.matcher(nxtString);


        Integer ONE = new Integer(1);

        // For each line
        while (lineMatcher.find()) {
            // Get line
            CharSequence line = lineMatcher.group();

            // Get array of words on line
            String words[] = wordBreakPattern.split(line);

            // For each word
            for (int i = 0, n = words.length; i < n; i++) {
                String nxtWord = words[i].trim();
                if (nxtWord.matches("[a-zA-Z]{2,}?")) {
                    nxtWord = nxtWord.toLowerCase();
                    Integer frequency = (Integer) 
map.get(nxtWord);
                    if (frequency == null) {
                        frequency = ONE;
                    } else {
                        int value = frequency.intValue();
                        frequency = new Integer(value + 1);
                    }
                    map.put(nxtWord, frequency);
                }
            }
        }

    }

Java2s ���� parseWordsFromString 使�� Java ������式�������址���
�龋���一��������式模式���诓��揖���末尾����为每�����蚀���一�� ONE ��
��������统�频��实某���频�省�然��循��通��每�����樱������诘��史指�模式�����臃�
��傻��省�����每���������址�牡��剩���统����储��频��映���小�
�������� RSS ��要��每�����实�频�剩��涂��越���������氐� Zoho Reports ����
�����恕�为�耍�����一���� �单 3 ����示��散��映�,���邪�����要�储�诒��械�
����荨�

�单 3. ����一������要����������莸�散��映��
                

        SimpleDateFormat dtFmt = new 
SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Set wordSet = map.keySet();
        for (Object objs : wordSet) {
            String nxtWord = (String) objs;
            Integer frequency = (Integer) map.get(nxtWord);

            HashMap rowValsMap = new HashMap();
            rowValsMap.put("Date", dtFmt.format(new Date()));
            rowValsMap.put("Word", nxtWord);
            rowValsMap.put("Frequency", frequency);
            Map result = reportClient.addRow(uri, rowValsMap, 
null);;
        }

循���械����一�����茫����帽���突��耍�����������值��拥����小���录�� Zoho 
Reports �看 WordFrequency ���械���荩��� 图 1 ��示�����看 ��图����

图 1. Zoho Reports �械� WordFrequency ��
 
为���酶帽��������茫�应�媒�一�����������斜?����说�����说����� http �� com 
之��某���省�
��页��
�呕����毓��
前一������每������一����� �� ��然������训姆����������������要���隙��时��
������荨���玫姆�����使������������一���������械���荨�目前��Zoho API 只��
��选�������� CSV �募���
CSV �募��母�式�芗虻ィ���一�邪��斜����斜?������邪���荨����� CSV �募�
时��������� POST ����指�����拥����夭�������芏�����������莸�选�,����
�����卤?追�拥����械谋?���吒��卤��械�����值������选�����细��息�� Zoho 
wiki �校��渭� �慰����希���
要�呕� RSS ���史������颍筛�����毓����使�� Zoho API import ����������
��莸��梅���一�� XML �牡�����细��录�����氐慕�� �� ���氐����������������
��息����也����一����������斜?����说一��值��������某一�胁���
要使�� import ��������������使�� opencsv���渭� �慰����希�����菁�转���� 
CSV �募���要����募�����循��通���映���械�值����每��值写���募��械�一�校���
�� �单 4����

�单 4. ���� CSV �募���������
                

        Calendar today = GregorianCalendar.getInstance();
                SimpleDateFormat formatter = new 
SimpleDateFormat("dd.MM.yy.H.mm.ss");
        String dateStr = formatter.format(today.getTime());
        String csvFileName = "benchmark-results" + dateStr + 
".csv";
        CSVWriter writer = new CSVWriter(new 
FileWriter(csvFileName));
        String[] columns = new 
String[]{"Date","Word","Frequency"};
        writer.writeNext(columns);

        SimpleDateFormat dtFmt = new 
SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String crntDateStr = dtFmt.format(new Date());
        Set wordSet = map.keySet();

        for (Object objs : wordSet) {
            String nxtWord = (String) objs;
            Integer frequency = (Integer) map.get(nxtWord);
            String[] values = new String[]{crntDateStr, nxtWord, 
frequency.toString()};
            writer.writeNext(values);
        }

        writer.close();


���� CSV �募�之��只要�� ReportClient �辖��募�����为����莞� 
importData() �������伞��梅����������氐� XML �牡��������玫������细�����
�� ImportResult �,�� �单 5 ��示��

�单 5. �呕� RSS ���史�����������
                

ReportClient reportClient = ZohoConfig.getReportClient();
        System.out.println(" Logging into the server");
        reportClient.login(ZohoConfig.LOGINNAME, 
ZohoConfig.PASSWORD);
        System.out.println(" Successfully logged in.");

        String uri = reportClient.getURI(ZohoConfig.LOGINNAME, 
                ZohoConfig.DATABASENAME, "WordFrequency");
        System.out.println("uri = " + uri);

        ImportResult importResult = 
                reportClient.importData(uri,"APPEND",new 
                        File(csvFileName),getImportConfig(), 
false);
        System.out.println("imported " + 
importResult.getTotalRowCount() + 
                " rows");

        reportClient.logout();

��牡�������使���������氐某�效������� 6000 ����莸����毓�蹋�时��蛹�
�������痰������印�
��页��
�� Zoho Reports ���� XML
����使�����址���之一�� Zoho Reports ������莸� XML��CSV ��图���式���募�
�小���一�址��������每�选�谋�准������指��直�拥������诙��址�����使��某�������� 
SQL SELECT ���亩�����
直�拥����� URI ��指�������谋?�������直�颖��到�募�����������单 6 �供
一�����樱�使����������直�拥����� XML �募������诒�����说����只�看��统��频��
���� 400 �牡��省�

�单 6. �� Zoho Reports ������ XML �募�
                

        String exportUri = 
reportClient.getURI(ZohoConfig.LOGINNAME, 
                ZohoConfig.DATABASENAME, "WordFrequency");
        File topXmlFile = new File("topFrequencyWords.xml");
        String xmlCriteria = "(\"Frequency\" > 400)";
        reportClient.exportData(exportUri, "XML", 
                topXmlFile, xmlCriteria, null);

�诒����校���指���约�只��要����频��值���� 400 ����荨����直�颖��到 
topFrequencyWords.xml �募����单 7 �供�舜� XML �募���一��片�巍�

�单 7. 摘�� topFrequencyWords.xml
                

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/flexanvil/BlogAnalyzer/WordFrequency" 
action="EXPORT">
<criteria>("Frequency" > 400)</criteria>
<result>
<rows>
<row>
<column name="Word">http</column>
<column name="Frequency">2687</column>
<column name="Date">19 May, 2009 16:43:26</column>
</row>
<row>
<column name="Word">com</column>
<column name="Frequency">2587</column>
<column name="Date">19 May, 2009 16:39:05</column>
</row>

�诘诙��������校�使�� SQL ��指����要��������荨����诒�����说��使��一些��� 
SQL ������������荨������要����频��值��平��值��统�������值�捅�准值������
使�������� �单 8 �械拇��。

�单 8. ��� Zoho Reports ���� SQL ��询
                

     String sqlUri = reportClient.getURI(ZohoConfig.LOGINNAME, 
             ZohoConfig.DATABASENAME);
        System.out.println("sqlUri = " + sqlUri);
        String sql = "SELECT avg(\"Frequency\") as Average, 
                count(\"Frequency\") as Count, 
max(\"Frequency\") as Maximum, " +
                "std(\"Frequency\") as Standard  FROM 
\"WordFrequency\"";
        File xmlFile = new File("frequencyData.xml");
        reportClient.exportDataUsingSQL(sqlUri, "XML", xmlFile, 
sql, null);

����指�� SQL �址������诘�����荨�SQL ���������菘�执�校����储�� XML 
�募� frequencyData.xml �校��� �单 9 ��示��

�单 9. 摘�� frequencyData.xml
                

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/flexanvil/BlogAnalyzer" action="EXPORT">
<result>
<rows>
<row>
<column name="Average">11.796</column>
<column name="Count">5834</column>
<column name="Maximum">2687</column>
<column name="Standard">78.0844</column>
</row>
</rows>
</result>
</response>

������莸� SQL �����丶��������冢�URI 没��指��使���母��?������ SQL ����
��隙�������荨����,�������一�������谋?�����谐�����要追�俚墓�司��然��
�����使��一�� SQL inner join ������玫��斜���每����司���到��频�剩��� ��
�� 10 ��示��

�单 10. ��� Zoho Reports ���械母��� SQL ��询������
                
        String companySql = "SELECT wf.Frequency, wf.Word, 
cm.Name 
                From WordFrequency wf inner join Companies cm on 
                wf.word = cm.name";
        File companyXmlFile = new File("companyFrequency.xml");
        reportClient.exportDataUsingSQL(sqlUri, "XML", 
                companyXmlFile, companySql, null);

����使�� SELECT ��询�������� WordFrequency �� Companies 之��� 
inner join�����械����公司��匹�。�单 11 展示�����懈貌�询����� XML ��
����

�单 11. SQL SELECT ��询��������
                

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/flexanvil/BlogAnalyzer" action="EXPORT">
<result>
<rows>
<row>
<column name="wf.Frequency">52</column>
<column name="wf.Word">apple</column>
<column name="cm.Name">Apple</column>
</row>
<row>
<column name="wf.Frequency">3</column>
<column name="wf.Word">sun</column>
<column name="cm.Name">Sun</column>
</row>
<row>
<column name="wf.Frequency">17</column>
<column name="wf.Word">intel</column>
<column name="cm.Name">Intel</column>
</row>
<row>
<column name="wf.Frequency">80</column>
<column name="wf.Word">microsoft</column>
<column name="cm.Name">Microsoft</column>
</row>
</rows>
</result>
</response>

����������些���佣����� XML����������院����捉�������苫�应�贸����小����,��
������ Java 应�贸����薪����� XML���员���示��应�贸����小�
��页��
�����偷���图��
Zoho Reports �亩�������之一�强��臃�������visual analyzer�������臃�����
��������俅�����莸�图�?图�魏������。��为一�����樱���������要����一
��图�危���示����频����叩�一些���省�图 2 展示�����诖���������图�目������
����visual designer�������看 ��图����

图 2. Zoho Reports ���臃������械�����图
 
������图之��通����� exportData() ������为���指��一��图���式����������
�掳��图��
        String imgURI = 
reportClient.getURI(ZohoConfig.LOGINNAME,
                ZohoConfig.DATABASENAME,"FrequencyChart");
        File imgFile = new File("FrequencyChart.png");
        
reportClient.exportData(imgURI,"IMAGE",imgFile,null,null);

exportData() ������图�伪���为 PNG �募���然�������使�酶� PNG �募�����应
�贸�������示图�巍�
��页��
������
���� Zoho Reports �供�� REST-ful �� Web ���瘢�����统应�贸��������
��������������������偷�����荨����� XML �����约�����预�����图�巍�������
�安装�����������俜�����莺筒�����������������越�����时�����诮��锌�
�����品���也为��������供������莺�执�锌��臃��������� �� 也�����品��������
�点。�������莶���强����品����伞�

--
Happy XMLing


�� ��源:��水木���� http://newsmth.net��[FROM: 114.249.215.*]