简体   繁体   中英

Iterate through XML Nodes on button click

I am brand new to Java programming but have done some work with c#. I am pulling some XML and have parsed it successfully I am also able to display eiter the First node or last node successfully but I need to iterate through the XML nodes on button press.

I am at a stand still trying to find a way to accomplish the iteration within my function, which is below:

 public void buttonPressed(View view)
    {
        // All static variables
        final String URL = "http://info4420w.ad.uvu.edu/it4420/10540595/Final_Project/Final_Project/quoterestservice.aspx";
        // XML node keys
        final String KEY_TABLE1 = "Table1"; // parent node
        final String KEY_QUOTE_ID = "QuoteID";
        final String KEY_CUST_NAME = "custName";
        final String KEY_CUST_PHONE = "custPhone";
        final String KEY_CUST_YEAR = "custYear";
        final String KEY_CUST_MAKE = "custMake";
        final String KEY_CUST_MODEL = "custModel";
        final String KEY_CUST_ISSUE = "custIssue";
        final String KEY_CUST_LOCLAT = "custLocLat";
        final String KEY_CUST_LOCLNG = "custLocLng";
        final String KEY_CUST_DESTLAT = "custDestLat";
        final String KEY_CUSTDESTLNG = "custDestLng";



        String quoteID = new String();
        String cName = new String();
        String cPhone = new String();
        String cYear = new String();
        String cMake = new String();
        String cModel = new String();
        String cIssue = new String();
        String cLocLat = new String();
        String cLocLng = new String();
        String cDestLat = new String();
        String cDestLng = new String();
        try {
            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element
            NodeList nl = doc.getElementsByTagName(KEY_TABLE1);

            quoteID = "";
            cName = "";
            cPhone = "";
            cYear = "";
            cMake = "";
            cModel = "";
            cIssue = "";
            cLocLat = "";
            cLocLng = "";
            cDestLat = "";
            cDestLng = "";

            // looping through all Table1 nodes <Table1> 
//          for (int i = 0; i < nl.getLength(); i++) {
                int i = 0;

                Element e = (Element) nl.item(i);
                quoteID = parser.getValue(e, KEY_QUOTE_ID); // name child value
                cName = parser.getValue(e, KEY_CUST_NAME); // cost child value
                cPhone = parser.getValue(e, KEY_CUST_PHONE); // description child value
                cYear = parser.getValue(e, KEY_CUST_YEAR);
                cMake = parser.getValue(e, KEY_CUST_MAKE);
                cModel = parser.getValue(e, KEY_CUST_MODEL);
                cIssue = parser.getValue(e, KEY_CUST_ISSUE);
                cLocLat = parser.getValue(e, KEY_CUST_LOCLAT);
                cLocLng = parser.getValue(e, KEY_CUST_LOCLNG);
                cDestLat = parser.getValue(e, KEY_CUST_DESTLAT);
                cDestLng = parser.getValue(e, KEY_CUSTDESTLNG);
                i++;
//          }
        } catch (Exception e) {
            cName = e.getMessage() + e.getStackTrace();
        }
        TextView tQuote = (TextView)findViewById(R.id.textView1);
        TextView tCust = (TextView)findViewById(R.id.textViewCust);
        TextView tIssue = (TextView)findViewById(R.id.textViewIssue);
        TextView tCar = (TextView)findViewById(R.id.textViewCar);
        TextView tLoc = (TextView)findViewById(R.id.textViewLoc);
        TextView tButton = (TextView)findViewById(R.id.button1);
        tButton.setText("Next Quote");
        tQuote.setText("Quote ID: " + quoteID );
        tCust.setText("Name: " + cName + "  Cell: " + cPhone);
        tIssue.setText("Issue: " + cIssue);
        tCar.setText("Make: " + cMake + "   Model: " + cModel + "   Year: " + cYear);
        tLoc.setText("Loc: " + cLocLat + ", " + cLocLng);

    }

My Button:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewLoc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:onClick="buttonPressed"
    android:text="Get Quote" />

Thank you in advance for your time.

你为什么不创建一个ArrayList保存所有的XML信息(比方说,一个ArrayList<MyObject> ,其中MyObjectPOJO ),那么当按钮被点击刚才设置的TextView s到列表中的下一个项目?

Might be an issue with this line:

TextView tButton = (TextView)findViewById(R.id.button1);

Are you casting a button to a TextView or is the button actually a TextView?

try this:

Button tButton = (Button)findViewById(R.id.button1);

As stated in the other answer, store each element in an ArrayList and retrieve whichever index you desire on button click. Try something like this (typing from mobile, hope this helps):

MyObject class :

public class MyObject {

public String quoteID;
public String name;
public String phone;
public int year;

//...the rest of any information you want stored in this object


} 

Then in your for-loop :

// Create a new ArrayList of MyObject
ArrayList<MyObject> elements = new ArrayList<MyObject>();
try {
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(KEY_TABLE1);

        quoteID = "";
        cName = "";
        cPhone = "";
        cYear = "";
        cMake = "";
        cModel = "";
        cIssue = "";
        cLocLat = "";
        cLocLng = "";
        cDestLat = "";
        cDestLng = "";

        // looping through all Table1 nodes <Table1> 
     for (int i = 0; i < nl.getLength(); i++) {
     // in your for-loop, create a new MyObject
     MyObject myObject = new MyObject();

            Element e = (Element) nl.item(i);
            quoteID = parser.getValue(e, KEY_QUOTE_ID); // name child value

           // set String objects of MyObject instance myObject
           myObject.quoteID = quoteID;

            cName = parser.getValue(e, KEY_CUST_NAME); // cost child value

            myObject.name = cName;
            // pass rest of info to myObject with below values from parser...
            cPhone = parser.getValue(e, KEY_CUST_PHONE); // description child value
            cYear = parser.getValue(e, KEY_CUST_YEAR);
            cMake = parser.getValue(e, KEY_CUST_MAKE);
            cModel = parser.getValue(e, KEY_CUST_MODEL);
            cIssue = parser.getValue(e, KEY_CUST_ISSUE);
            cLocLat = parser.getValue(e, KEY_CUST_LOCLAT);
            cLocLng = parser.getValue(e, KEY_CUST_LOCLNG);
            cDestLat = parser.getValue(e, KEY_CUST_DESTLAT);
            cDestLng = parser.getValue(e, KEY_CUSTDESTLNG);

            //then finally, add myObject to ArrayList
            elements.add(myObject);
            i++;
//          }
    } catch (Exception e) {
        cName = e.getMessage() + e.getStackTrace();
    }
    TextView tQuote = (TextView)findViewById(R.id.textView1);
    TextView tCust = (TextView)findViewById(R.id.textViewCust);
    TextView tIssue = (TextView)findViewById(R.id.textViewIssue);
    TextView tCar = (TextView)findViewById(R.id.textViewCar);
    TextView tLoc = (TextView)findViewById(R.id.textViewLoc);
    TextView tButton = (TextView)findViewById(R.id.button1);
    tButton.setText("Next Quote");
    tQuote.setText("Quote ID: " + quoteID );
    tCust.setText("Name: " + cName + "  Cell: " + cPhone);
    tIssue.setText("Issue: " + cIssue);
    tCar.setText("Make: " + cMake + "   Model: " + cModel + "   Year: " + cYear);
    tLoc.setText("Loc: " + cLocLat + ", " + cLocLng);

}

Finally, it can be retrieved by index value:

MyObject myObject = elements.get(index):

Hope this helps, happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM