I've parsed an xml file, but I need to further split a string within it, how do i do this?

69 Views Asked by At

I have parsed an XML feed, but within one of the tags I wish to further parse the data, but am unsure of how to do this? I'm utilising XMLPullParser for this in a package, then the information is stored in a Widgetclass package, before being sent to my mainactivity through an inent using a broadcast receiver.

currently the code is only calling one field of the parsed information, but calling more presents the problem of the unsorted XML Tag's contents. I cant seem to get a split.String method to work here, should it be in the widget class I've got to store the information or in the mainactivity?

//the parser public static WidgetClass[] parseFeed(String content) {

    try {

        boolean inItemTag = false;
        String currentTagName = "";
        WidgetClass currentItem = null;
        List<WidgetClass> itemList = new ArrayList<>();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new StringReader(content));

        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    currentTagName = parser.getName();
                    if (currentTagName.equals("item")) {
                        inItemTag = true;
                        currentItem = new WidgetClass();
                        itemList.add(currentItem);
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("item")) {
                        inItemTag = false;
                    }
                    currentTagName = "";
                    break;

                case XmlPullParser.TEXT:
                    String text = parser.getText();
                    if (inItemTag && currentItem != null) {
                        try {
                            switch (currentTagName) {
                                case "title":
                                    currentItem.setTitle(text);
                                    break;
                                case "description":
                                    currentItem.setDescription(text);
                                    break;
                                case "link":
                                    currentItem.setLink(text);
                                    break;
                                case "pubDate":
                                    currentItem.setPubDate(text);
                                    break;
                                case "category":
                                    currentItem.setCatagory(text);
                                    break;
                                case "geo:lat":
                                    currentItem.setGeoLat(Double.parseDouble(text));
                                case "geo:long":
                                    currentItem.setGeoLong(Double.parseDouble(text));
                                default:
                                    break;
                            }
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            eventType = parser.next();

        } // end while loop


       WidgetClass[] widgetClasses = new WidgetClass[itemList.size()];
        return itemList.toArray(widgetClasses);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

Ideally I'd like to further split the stream from the tag, then put them into the array to pass on to my visual layer.

1

There are 1 best solutions below

0
Ferran On BEST ANSWER

Your string example:

<description> Origin date/time: Thu, 14 Feb 2019 07:43:33 ; Location: NEWDIGATE,SURREY ; Lat/long: 51.163,-0.250 ; Depth: 2 km ; Magnitude: 2.4 </description> 

As you can see, all parts of strings are separated by ';'. In this case, you can use this char to split the string:

String[] textParts = text.split(";");

Then, each part contains the desired part of the string

partsCounts = textParts.length; // number of parts
textParts[0] => " Origin date/time: Thu, 14 Feb 2019 07:43:33 "
textParts[1] => " Location: NEWDIGATE,SURREY "
textParts[2] => "  Lat/long: 51.163,-0.250 "
textParts[3] => " Depth: 2 km "
testParts[4] => "  Magnitude: 2.4 "

If you are editing also the xml file, I recommend you to edit it like this

<description> 
    <origin>Thu, 14 Feb 2019 07:43:33</origin>
    <location>NEWDIGATE,SURREY</location>
    <lat>51.163,-0.250</lat>
    <depth>2 km</depth>
    <magnitude>2.4</magnitude>
</description>

So, you can access each part directly with XmlPullParser