简体   繁体   中英

ANDROID XMLPULLPARSER Parsing Inbetween tags

I am new to android and I am trying to understand XMLPULLPARSER. My current code I have managed to parse inbetween tags.

Heres the snippet I am using to parse with which works fine.

case XmlPullParser.START_TAG:  
                    if (tagname.equalsIgnoreCase("employee")) {  
                        // create a new instance of employee  
                        employee = new Employee();  
                    }  
                    break;  

                case XmlPullParser.TEXT:  
                    text = parser.getText();  
                    break;  

                case XmlPullParser.END_TAG:  
                    if (tagname.equalsIgnoreCase("employee")) {  
                        // add employee object to list  
                        employees.add(employee);  
                    }else if (tagname.equalsIgnoreCase("id")) {  
                        employee.setId(Integer.parseInt(text));  
                    }  else if (tagname.equalsIgnoreCase("name")) {  
                        employee.setName(text);  
                    } else if (tagname.equalsIgnoreCase("salary")) {  
                        employee.setSalary(Float.parseFloat(text));  
                    }   
                    break; 

But an example of the XML is

<name> First name: Bob <br /> Last name: John </name>

I am looking to extract first name and last name separately. I already have the setters and setters.

How would i go about this?

You would need to parse your text String and manually extract the first and last name from it.

Note that this is not a good XML design. Rather than combining last name and first name in a string (with prefixes and html code like <br/> thrown in) you should introduce own XML elements for last name and first name, for instance:

<firstname>Bob</firstname>
<lastname>John</lastname>

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