用户如何搜索解析的Xml并显示输出

时间:2016-02-21 12:46:34

标签: java android xml search

我已经成功创建了一个Xml Parser,它在TextView中输出整个Xml。但是我找不到用户输入搜索的方法,因此可以在TextView中输出答案之前搜索Xml的输入。有人可以帮忙吗?

public void Species() {
    Common = "Not set";
    Latin = "Not set";
    Where = "Not set";
    Kingdom = "Not set";
    Phylum = "Not set";
    Class = "Not set";
    Order = "Not set";
    Family = "Not set";
    Genus = "Not set";
    Scientific = "Not set";
}

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button;
    //private XmlPullParser parser;
    private EditText search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        search = (EditText) findViewById(R.id.editText);
        search.setOnClickListener(this);


    }


    private void Xmlreader() {
        try {
            //parser = Xml.newPullParser();
            //InputStream in_s = getApplicationContext().getAssets().open("classification.xml");
            XmlPullParser parser = getResources().getXml(R.xml.classification);
            //Toast.makeText(MainActivity.this, "Xml opened for Parsing", Toast.LENGTH_SHORT).show();
            //parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            //Toast.makeText(MainActivity.this, "XXXXXXXXXXXXXXXXX", Toast.LENGTH_SHORT).show();
            //parser.setInput(in_s, null);
            parseXml(parser);
        } catch (XmlPullParserException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }


    private void parseXml(XmlPullParser parser) throws
            XmlPullParserException, IOException {

        //Toast.makeText(MainActivity.this, "parseXml", Toast.LENGTH_SHORT).show();
        ArrayList<Species> classification = null;
        int eventType = parser.getEventType();
        Species currentSpecies = null;
        //Toast.makeText(MainActivity.this, "got first event type", Toast.LENGTH_SHORT).show();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            String name = null;
            String temp = null;
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    //Toast.makeText(MainActivity.this, "Document Started", Toast.LENGTH_SHORT).show();
                    classification = new ArrayList();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    temp = "Name found: " + name;
                    //Toast.makeText(MainActivity.this, temp, Toast.LENGTH_SHORT).show();
                    if (name.trim().equalsIgnoreCase("species")) {
                        //Toast.makeText(MainActivity.this, "New species", Toast.LENGTH_SHORT).show();
                        currentSpecies = new Species();
                    } else if (currentSpecies != null) {
                        if (name.trim().equalsIgnoreCase("Common")) {
                            currentSpecies.Common = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Latin")) {
                            currentSpecies.Latin = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Where")) {
                            currentSpecies.Where = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Kingdom")) {
                            currentSpecies.Kingdom = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Phylum")) {
                            currentSpecies.Phylum = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Class")) {
                            currentSpecies.Class = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Order")) {
                            currentSpecies.Order = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Family")) {
                            currentSpecies.Family = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Genus")) {
                            currentSpecies.Genus = parser.nextText();
                        } else if (name.trim().equalsIgnoreCase("Scientific")) {
                            currentSpecies.Scientific = parser.nextText();
                        }
                        //Toast.makeText(MainActivity.this, "1st Sequence of Xml Parsed", Toast.LENGTH_SHORT).show();

                    }
                    break;

                case XmlPullParser.END_TAG:
                    //Toast.makeText(MainActivity.this, "End Tag", Toast.LENGTH_SHORT).show();
                    //name = parser.getName();
                    //if (name.equalsIgnoreCase("species") && currentSpecies != null) {
                    if (currentSpecies != null) {
                        //Toast.makeText(MainActivity.this, "Adding Species", Toast.LENGTH_SHORT).show();
                        classification.add(currentSpecies);
                    }

            }
            eventType = parser.next();
        }

        printClassification(classification);

    }

    private void printClassification(ArrayList<Species> classification) {
        //Toast.makeText(MainActivity.this, "Print Classification", Toast.LENGTH_SHORT).show();
        String content = "";
        Iterator<Species> it = classification.iterator();
        while (it.hasNext()) {
            Species currSpecies = it.next();
            content = content + "Common Name: " + currSpecies.Common + "\n\n";
            content = content + "Latin Name: " + currSpecies.Latin + "\n\n";
            content = content + "Where: " + currSpecies.Where + "\n\n";
            content = content + "Kingdom: " + currSpecies.Kingdom + "\n\n";
            content = content + "Phylum: " + currSpecies.Phylum + "\n\n";
            content = content + "Class: " + currSpecies.Class + "\n\n";
            content = content + "Order: " + currSpecies.Order + "\n\n";
            content = content + "Family: " + currSpecies.Family + "\n\n";
            content = content + "Genus: " + currSpecies.Genus + "\n\n";
            content = content + "Scientific: " + currSpecies.Scientific + "\n\n";
        }
        TextView display = (TextView) findViewById(R.id.info);
        EditText search = (EditText) findViewById(R.id.editText);
        search.getText();
        display.setText(content);
    }


    @Override
    public void onClick(View view) {
        Xmlreader();

    }
}

0 个答案:

没有答案