开发者

Looping through XML to build an object

So what im trying to do is to loop through my XML file shown below and save the contents into Objects for each "screen".

My XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<screens>
    <screen id="house" backdropimage = "R.drawable.scene">
        <region id="one" rleft = "200" rright = "200" rtop = "10" rbottom = "10"/>
        <region id="two" rleft = "220" rright = "220" rtop = "12" rbottom = "12"/>
        <paths left = "park" right = "x" top = "x" bottom = "x"/>
    </screen>
    <screen id="park" backdropimage = "R.drawable.park">
        <region id="1" rleft = "500" rright = "200" rtop = "10" rbottom = "10"/>
        <paths right = "house" left = "x" top = "x" bottom = "x"/>
    </screen>
</screens>

So the idea is that I create an Object in my array of screen with the information in each screen block. The id and backdrop are fine but im having problems iterating through the 'region' parts as there will be different amounts of region entries in different sections. This is the code im using currently ut for some reason it goes through 'one' 'two' and '1' where i would like it to stop at /screen.

    depth = parser.getDepth();
            while(parser.getDepth() == depth) { 

                if(event==XmlPullParser.START_TAG && parser.getName().contentEquals("region")) {
                    Region reg = new Region(parser.getAttributeIntValue(null, "rleft", 0),
                            parser.getAttributeIntValue(null, "rtop", 0),
                            parser.getAttributeIntValue(null, "rright", 0),
                            parser.getAttributeIntValue(null, "rbottom", 0));
                    //String str = parser.getIdAttribute();
                    newScreen.area = (reg);
                    //Rect temp = reg.getBounds();
                    newScreen.areaArray.add(reg);
            parser.next();
            }   

This is the first time I've tried using XML files in java/android and I'm still pretty new to Java on the whole so I'm sure I'm not using the 开发者_StackOverflowbest method here and would appreciate any help. Thanks


I use Simple-xml to serialize my xml to an Object. They have pretty good documentation, you can get the library here: http://simple.sourceforge.net/

Here's an example:

YourActivity.java

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

....
private Screens myScreens;

....


private void serializeMyXml(){
        File source = new File("directory/yourXmlFile.xml");
        Serializer serializer = new Persister();
        try {
            myScreens= serializer.read(Screens.class, source);

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

Screens.java

import java.io.Serializable;
import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root
public class Screensimplements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1111;//some random unique serialversion id

    @ElementList(inline=true)
    private List<Screen> list;

    public List<Screen> getList() {
        return list;
    }

}

Screen.java

    import java.io.Serializable;

    import org.simpleframework.xml.Attribute;

    public class Entity implements Serializable{

        /**
         * 
         */
        private static final long serialVersionUID = 5004451486040850151L;

        @Attribute
        private String backdropimage;

        @Attribute
        private int id;

        @ElementList
        private List<Region> region;

@Element
private Paths paths;

        public int getId() {
            return id;
        }

        public String getBackdropimage() {
            return backdropimage;
        }

    }

And so on.. and so on.. read more about it in the documewntatio.

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜