1

this is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<frames>
<frame>
    <name>FRAME-A</name>
    <coordinatesA>
       <x>75</x>
       <y>75</y>
    </coordinatesA>
    <coordinatesB>
       <x>75</x>
       <y>490</y>
    </coordinatesB>
    <coordinatesC>
       <x>645</x>
       <y>75</y>
   </coordinatesC>
    <coordinatesD>
       <x>1215</x>
       <y>75</y>
    </coordinatesD>
    <coordinatesE>
        <x>0</x>
       <y>0</y>
    </coordinatesE>
    <image>@drawable/frameguideone</image>
</frame>

How to parse tags x and y inside coordinatesA B C D E tag? I have no idea how to do this. Anyone can help me with this problem? I looked for it in google but didn't get my solution.

this is my java parsing:

public class XMLPullParserHandler {
List<Employee> frames;
private Employee frame;
private String text;

public XMLPullParserHandler() {
    frames = new ArrayList<Employee>();
}

public List<Employee> getEmployees() {
    return frames;
}

public List<Employee> parse(InputStream is) {
    XmlPullParserFactory factory = null;
    XmlPullParser parser = null;
    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newPullParser();

        parser.setInput(is, null);

        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagname = parser.getName();
            switch (eventType) {
            case XmlPullParser.START_TAG:
                if (tagname.equalsIgnoreCase("frame")) {
                    // create a new instance of employee
                    frame = new Employee();
                }
                break;

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

            case XmlPullParser.END_TAG:
                if (tagname.equalsIgnoreCase("frame")) {
                    // add employee object to list
                    frames.add(frame);
                } else if (tagname.equalsIgnoreCase("name")) {
                    frame.setName(text);
                } else if (tagname.equalsIgnoreCase("coordinates")) {
                    frame.setCoordinates(Integer.parseInt(text));
                } else if (tagname.equalsIgnoreCase("image")) {
                    frame.setImage(text);
                }else if(tagname.equalsIgnoreCase("x")){
                    frame.setX(Integer.parseInt(text));
                }else if(tagname.equalsIgnoreCase("y")){
                    frame.setY(Integer.parseInt(text));
                }
                break;
            default:
                break;
            }
            eventType = parser.next();
        }

    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return frames;
}

}

JAC
  • 119
  • 12

2 Answers2

0

When you read the start tag of coordinates, create a new coordinate object. As you read x and y, set the values on the coordinate. When you read the end coordinate tag, set the coordinate on the frame.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
0

There are two problems with your code.

First, this won't work :

if (tagname.equalsIgnoreCase("coordinates")) {
    frame.setCoordinates(Integer.parseInt(text));
}

You must have either the same name as the xml tag or use tagname.contains("coordinates").

Second, x and y will always be equal to 0 because you are erasing the previous value with a new one. So your Employee object should contains a list of Coordinates and everytime you meet the coordinate tag, you should create a new Coordinate object, set x and y and add it to the list. Something like this should work :

case XmlPullParser.START_TAG:
    if (tagname.equalsIgnoreCase("frame")) {
        frame = new Employee();
    } 
    else if (tagname.contains("coordinates")) {
        frame.addCoordinate(new Coordinate());
    } 
    break;

case XmlPullParser.END_TAG:
    if (tagname.equalsIgnoreCase("x")) {
        // get the coordinate object created in the START_TAG case
        frame.getLastCoordinates().setX(Integer.parseInt(text));
    }
    else if (tagname.equalsIgnoreCase("y")) {
        frame.getLastCoordinates().setY(Integer.parseInt(text));
    }

Hope it helped !

lcw_gg
  • 679
  • 5
  • 14
  • Yes, it's supposed to add an element in your Coordinate list. – lcw_gg Jan 16 '15 at 07:04
  • hi yes i already add it, but How can i do the getLastCoordinates()? I already seen it in logcat but when i try to put it in my listview it's shown 0 Thankyou. – JAC Jan 16 '15 at 07:12
  • if you use an ArrayList for example, to get the last element, something like this would work : `public Coordinates getLastCoordinates(){ return coordinatesList.get(coordinatesList.size() -1); }` It's one of the way of doing it, maybe there is a more efficient way to do it. – lcw_gg Jan 16 '15 at 08:04
  • Thankyou i add for loop :) – JAC Jan 16 '15 at 08:05