2013年9月21日 星期六

對 String ( 內容為 xml 標籤結構 ) , 使用特定 Tag 標籤 , 截取 substring 標籤的值


    String MyStream = "
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <TraExtendCar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Placemark>
            <SN>1</SN>
            <City>臺北市</City>
            <Area>南港區</Area>
            <Description>忠孝東路六段39號前(台電前)</Description>
            <Lat>25.04665</Lat>
            <Long>121.58366</Long>
            <Speed>50</Speed>
            <Extend>10</Extend>
            <Bearing>227</Bearing>
        </Placemark>
        <Placemark>
            <SN>2</SN>
            <City>臺北市</City>
            <Area>南港區</Area>
            <Description>忠孝東路六段188巷(東向西)</Description>
            <Lat>25.048832</Lat>
            <Long>121.587055</Long>
            <Speed>50</Speed>
            <Extend>10</Extend>
            <Bearing>255</Bearing>
        </Placemark>
            ";
// 以上 String 的建立 , 不是正確的 , 只是個方便看到內容的範例
   
    String Tag = "Description";
    String StartTag = "<" + Tag + ">";
    String EndTag = "</" + Tag + ">";
    int iStart = 0;
    int iEnd = 0;
    String strOut = "";
    iStart = MyStream.indexOf(StartTag)+StartTag.length();
    iEnd = MyStream.indexOf(EndTag);
    strOut = MyStream.substring(iStart, iEnd);










截取出 , 與 Tag 相同的標簽裡頭的 值 , 值會透過 substring 存於 strOut 裡
因來源應該是 xml 之類的文字檔 ,
值 的預設形態會是 String ,



以上不是 Bug Free , 還需要做 檢查 是否 標籤成對 的存在 ,
產生錯誤的測試為 , 使用不存在的標籤去跑程式 , 如
    String Tag = "Desc %$@ ription";

會產生 java.lang.NullPointerException 的錯誤









打上斷點(Toggle Line Breakpoint) , 透過 Debug 執行(Run下面那一個) ,
觀察 (x) = Variables ,
可見到 String indexOf 如果沒有找到 標籤 , 會回傳 -1 的值 ,





一個簡單的方法 , 用 try catch 包起來 ,
    try
    {
        String Tag = "Desc %$@ ription";
        String StartTag = "<" + Tag + ">";
        String EndTag = "</" + Tag + ">";
        int iStart = 0;
        int iEnd = 0;
        String strOut = "";
        iStart = MyStream.indexOf(StartTag)+StartTag.length();
        iEnd = MyStream.indexOf(EndTag);
        strOut = MyStream.substring(iStart, iEnd);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

程式可以繼續往下走 , 不會報錯而中斷



純文字檔(xml為例) , 放置於 assets , 使用 AssetManager 讀取 , 輸出到 TextView

        TextView info_message = (TextView)findViewById(R.id.info_message);
AssetManager assetManager = getAssets();
    InputStream inputStream = null;
 
    String MyStream;
    try {
        // 指定/assets/MyAssets.txt
        inputStream = assetManager.open("trappoint.xml");
   
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[4096];
   
        int len;
        while ((len = inputStream.read(bytes)) > 0){
         byteArrayOutputStream.write(bytes, 0, len);
        }
   
        MyStream = new String(byteArrayOutputStream.toByteArray(), "UTF8");
       
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        MyStream = e.toString();
    }
 
    info_message.setText(MyStream);


使用  AssetManager , 將放置於 assets(資源管理匣) 的 trappoint.xml(各種檔案)
在此以 純文字檔 xml 為例
讀取 , 顯示於 名為 R.id.info_message 的 TextView



TextView 輸出結果