久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

xml dom 解析器然后使用列表視圖顯示結果

xml dom parser then display the result with list-view(xml dom 解析器然后使用列表視圖顯示結果)
本文介紹了xml dom 解析器然后使用列表視圖顯示結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 web 服務,它有方法返回 xml 字符串給我我想解析這個 xml 然后用列表視圖顯示它.

I have web service that has method return string of xml to me i want to parse this xml then display it with list-view .

主要活動

    public class MainActivity extends ListActivity {
    // All static variables
   format=xml";
    // XML node keys
    static final String KEY_ITEM = "Roomtype"; // parent node
    static final String KEY_ID = "IDRoom";
    static final String KEY_NAME = "Code";
    static final String KEY_COST = "Normbed";
    static final String KEY_DESC = "Maxbed";
    static final String KEY_categroy = "Category";
    static final String KEY_order = "Order";

    ProgressBar pg;
    String spin1;
    String spin2;
    String spin3;
    String displayText;
    TextView tv;
    ListView lv;
    int day, month, year;
    ArrayList<HashMap<String, String>> menuItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
        pg = (ProgressBar) findViewById(R.id.progressBar1);
        spin1 =  spinner1.getSelectedItem().toString();
        spin2 =  spinner2.getSelectedItem().toString();
        spin3 =  spinner3.getSelectedItem().toString();


        //Search
        Button btn_search=(Button)findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create instance for AsyncCallWS
                AsyncCallWS task = new AsyncCallWS();
                //Call execute
                AsyncTask<String, Void, Void> xx=task.execute();

            }
        });
    }
private void parsexml(String xmlresult){

    ListView myList=(ListView)findViewById(android.R.id.list);

    menuItems = new ArrayList<HashMap<String, String>>();

    XmlParser parser = new XmlParser();
    String xml = null; // getting XML
    try {
        xml = parser.getXmlFormUrl(xmlresult);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID,  parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
        map.put(KEY_DESC, parser.getValue(e, KEY_categroy));
        map.put(KEY_DESC, parser.getValue(e, KEY_order));
        // adding HashList to ArrayList
        menuItems.add(map);
    }
    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST,KEY_categroy,KEY_order }, new int[] {
            R.id.name, R.id.desciption, R.id.cost,R.id.categroy,R.id.order });

    setListAdapter(adapter);

}

    private class AsyncCallWS extends AsyncTask<String, Void, Void> {
        private Exception exception;
        @Override
        protected Void doInBackground(String... params) {
            try {
                //Invoke webservice
                displayText = WebService.invokeHelloWorldWS(spin2, spin3, spin1, "search_availability");
                parsexml(displayText);

            }catch (Exception e) {
                this.exception = e;
                return null;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            //Make ProgressBar invisible
            pg.setVisibility(View.INVISIBLE);
        }

        @Override
        protected void onPreExecute() {
            //Make ProgressBar invisible
            pg.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }

    }

}

XmlParser 類

public class XmlParser {

    String result;
    public String getXmlFormUrl(String link) throws IOException {
        URL url=new URL(link.toString());
        HttpURLConnection UrlConnection= (HttpURLConnection) url.openConnection();
        int status=UrlConnection.getResponseCode();
        if(status==200){
            InputStream inputStream=UrlConnection.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF8"));
            StringBuilder stringBuilder= new StringBuilder();
            String line;
            while ((line=bufferedReader.readLine())!=null){
                stringBuilder.append((line+"
"));
            }
            result=stringBuilder.toString();
            inputStream.close();
        }
        return  result;
    }
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        // return DOM
        return doc;
    }
    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }

    public final String getElementValue( Node elem ) {
        Node child;
        if( elem != null){
            if (elem.hasChildNodes()){
                for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                    if( child.getNodeType() == Node.TEXT_NODE  ){
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }
}

當我點擊搜索按鈕時,這是 xml 文件的主文件,結果將顯示.MainActivity.xml

this is the xml file main when i tab on search button the result will shown . MainActivity.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_arrival"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Arrival Date"/>

        <Button
            android:id="@+id/btn_arrival"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Set"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_departure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Departure Date"/>

        <Button
            android:id="@+id/btn_departure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Set"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Persons"/>
        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown"
            android:entries="@array/array_person"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="child"/>
        <Spinner
            android:id="@+id/spinner3"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown"
            android:entries="@array/array_child"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Rooms"/>
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown"
            android:drawSelectorOnTop="true"
            android:entries="@array/array_rooms"
            />
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Login"/>
        <Button
            android:id="@+id/btn_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Search"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@android:id/list"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="73dp" />
    </RelativeLayout>
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="invisible"
        />
</LinearLayout>

List_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- Name Label -->
        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#dc6800"
            android:textSize="16sp"
            android:textStyle="bold"
            android:paddingTop="6dip"
            android:paddingBottom="2dip" />
        <!-- Description label -->
        <TextView
            android:id="@+id/desciption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#acacac"
            android:paddingBottom="2dip">
        </TextView>
        <!-- Linear layout for cost and price Cost: Rs.100 -->
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!-- Cost Label -->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#5d5d5d"
                android:gravity="left"
                android:textStyle="bold"
                android:text="Cost: " >
            </TextView>
            <!-- Price Label -->
            <TextView
                android:id="@+id/cost"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#acacac"
                android:textStyle="bold"
                android:gravity="left">
            </TextView>
            <TextView
                android:id="@+id/categroy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#acacac"
                android:textStyle="bold"
                android:gravity="left">
            </TextView>
            <TextView
                android:id="@+id/order"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#acacac"
                android:textStyle="bold"
                android:gravity="left">
            </TextView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

推薦答案

您正在嘗試使用 WebService.invokeHelloWorldWS(spin2, spin3, spin1, "search_availability"); 更改您的代碼以解析 xml.

You are trying to get again the xml result using the xml results from WebService.invokeHelloWorldWS(spin2, spin3, spin1, "search_availability"); change your code to parse the xml instead.

例子:

private void parsexml(String xmlresult){

    ListView myList=(ListView)findViewById(android.R.id.list);

    menuItems = new ArrayList<HashMap<String, String>>();

    XmlParser parser = new XmlParser();
    Document doc = parser.getDomElement(xmlresult); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID,  parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
        map.put(KEY_DESC, parser.getValue(e, KEY_categroy));
        map.put(KEY_DESC, parser.getValue(e, KEY_order));
        // adding HashList to ArrayList
        menuItems.add(map);
    }
    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST,KEY_categroy,KEY_order }, new int[] {
            R.id.name, R.id.desciption, R.id.cost,R.id.categroy,R.id.order });

    myList.setListAdapter(adapter);
    ((BaseAdapter)adapter).notifyDataSetChanged();

}

別忘了在setListAdapter(adapter)onPostExecute()notifyDataSetChanged()中執(zhí)行parsexml()/代碼>;

Don't forget to execute parsexml() in onPostExecute() and notifyDataSetChanged() after setListAdapter(adapter);

這篇關于xml dom 解析器然后使用列表視圖顯示結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(fā)(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 亚洲在线看| 国产日产欧产精品精品推荐蛮挑 | 欧美视频精品 | 精品乱码一区二区三四区视频 | 视频一区二区中文字幕 | 亚洲综合第一页 | 综合天天久久 | 成人一区二区三区 | 国产欧美在线播放 | 亚洲日本欧美日韩高观看 | 亚洲国产精品成人无久久精品 | 99久久久久国产精品免费 | 色一情一乱一伦一区二区三区 | 久久久久一区 | 欧美人人| 欧美四虎 | 香蕉久久网 | 欧美一级黑人aaaaaaa做受 | 亚洲成av人片在线观看无码 | 蜜桃精品在线 | 99综合| 国产激情综合五月久久 | 国产99久久精品一区二区永久免费 | 精品国产一区二区三区性色av | 国产999精品久久久影片官网 | 欧美久久久久 | 久久黄视频 | 麻豆久久久 | 一区二区三区四区五区在线视频 | 国产一区欧美 | 怡红院免费的全部视频 | 91五月婷蜜桃综合 | 欧美另类视频 | 美女一级毛片 | 国产中文视频 | 久久久久亚洲 | 亚洲毛片在线观看 | 成人性视频在线播放 | 欧美1区 | 一区视频在线 | 国产精品久久久久久久久免费丝袜 |