簡體   English   中英

使用FileInputStream讀取xml文件

[英]reading xml file using FileInputStream

我正在使用下面給出的代碼讀取XML文件,但是每次讀取XML並打印該XML ,總是缺少XML的開始標記,這反過來會產生錯誤。

讀取xml的代碼:

package com.vaannila.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vaannila.FormBean.XmlRetrivalForm;

public class AppletRefeshAction  extends Action {
    String filepath = "C:/Users/ashutosh_k/idoc/docRuleTool/WebContent/data/Malaria.xml";
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        XmlRetrivalForm xrf = (XmlRetrivalForm)form;
        String dieasename =xrf.getDieseasename();
        System.out.println("ghhjhhjkh"+dieasename);
        /* this part of the code is from jsp */
        //FileReader fis = new FileReader(new File(filepath));
        FileInputStream fis = new FileInputStream(new File(filepath));
        byte bin[] = new byte[(int) new File(filepath).length()];
        while(fis.read()>0){
        fis.read(bin);
        }
        System.out.println("I am here after reading the xml part of it ");
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        response.getOutputStream().write(bin);
//      response.getWriter().write(bin);
        String ashu = new String(bin);
        String iam =ashu.trim();
        System.out.println("i am in the new action class now ");
        System.out.println(iam);
        fis.close();
        /* this part of the code jsp ends here */
        return null;
    }

}

輸出xml生成:

?xml version="1.0" encoding="UTF-8" standalone="no"?><tree>
<declarations>
    <attributeDecl name="name" type="String"/>
</declarations>
<branch>
<branch>
<attribute name="name" value="Excess Stool"/>
<branch>
<branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/>
<branch>
<attribute name="name" value=""/></branch>
<branch>
<attribute name="name" value=""/></branch>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have watery stool"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
</branch>
<branch>
<branch>
<attribute name="name" value="Was your peak temperature greater than 104"/>
<branch>
<branch>
<attribute name="name" value="Do you have chills"/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="High Fever"/></branch>
<branch>
<branch>
<attribute name="name" value="Do you have pain in the left part of the head "/>
<branch>
<branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Bodyache"/></branch>
<attribute name="name" value="Malaria"/></branch>
</tree>

為什么這個?

    FileInputStream fis = new FileInputStream(new File(filepath));
    byte bin[] = new byte[(int) new File(filepath).length()];
    while(fis.read()>0){
    fis.read(bin);
    }

而不是以此來閱讀文字?

BufferedReader reader = new BufferedReader(new FileReader(filepath));
String line = null;
StringBuilder builder = new StringBuilder();
while ( (line = reader.readLine()) != null)
    builder.append(line);
reader.close();

首先,您在每次循環迭代中以相同的偏移量讀取字節緩沖區-您不能期望總是一次讀取整個文件。 其次,不要在字節流和字符流之間進行不必要的轉換-您應該將XML讀取為字節,然后“轉發”為字節。 請注意如何在字節數組和字符串之間進行轉換-請記住,新的String(byte[])使用平台默認的字符編碼(可能不是utf-8)將字節轉換為字符。

// read the file
ByteArrayOutputStream out = new ByteArrayOutputStream(10000);
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
int c;
while ((c = in.read()) != -1)
    out.write(c);
in.close();

// debug as string
if (debug) {
   System.out.writeln(new String(out.toBytes(), Charset.forName("utf-8"));
}


// forward to client through response
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
response.getOutputStream().write(out.toBytes());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM