如何使用xmlpullparser在android中解析这个自定义xml

时间:2011-11-24 20:02:55

标签: android xml xmlpullparser

我有这个xml:

  <cars>
   <a b="car" c="blue">

    <x year="01"/>
    <x year="03"/>

   </a>

   <a b="truck" c="red">
    <x year="04"/>
    <x year="85"/>
   </a>

</cars>

我想解析一个像这样的对象(arraylist):

01:["car", "blue", "01, 03"]

02:["truck", "red", "04, 85"]

请注意,两年的atrributes在同一个String中一起出现。这是我无法弄清楚的。

我使用的解析器是android原生XMLPullParser

我无法更改XML格式,但如果它值得,我可以使用另一个兼容Android的解析器。

如果不清楚它必须适合这样的类:

private String car;
private String color;
private String years;


public ClassVehicle(String aCar, String c, String ys) {
    this.orden = aCar;
    this.intext = c;
    this.lugar = ys;


}

 getters & setters toString() and so on

最终结果将与汽车一样多的arraylists(对象):

 ArrayList<ClassCar> oCars = new ArrayList<ClassCar>();

oCars.add(new ClassCar(car, color, years));

1 个答案:

答案 0 :(得分:2)

您的代码段不是XML ...请参阅Extensible Markup Language (XML) 1.1 (Second Edition)

  • c属性值未关闭。
  • x标签未关闭。
  • 你有多个根...

如果这些是拼写错误,你应该纠正错误,我会检查一下是否可以提供真正的答案......

作为一名负责任的程序员,请不要使用该格式,而是将数据迁移到定义良好的格式,但不要在客户端应用程序上使用这种损坏的格式。

<强>更新

这是一个快速而肮脏的实现(使用我自己的Vehicle POJO):

public class Butelo extends Activity
{
    public static String TAG = "SO Butelo";

    public static List<Vehicle> vehicles = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput( new StringReader ( "<cars><a b=\"car\" c=\"blue\"><x year=\"01\"/><x year=\"03\"/></a><a b=\"truck\" c=\"red\"><x year=\"04\"/><x year=\"85\"/></a></cars>") );

            int eventType = xpp.getEventType();
            boolean done = false;

            Vehicle currentVehicle = null;

            while (eventType != XmlPullParser.END_DOCUMENT && !done) {

                String name = null;
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        vehicles = new ArrayList<Vehicle>();
                        break;
                    case XmlPullParser.START_TAG:
                    name = xpp.getName();
                        if (name.equals("a")){
                            currentVehicle = new Vehicle();
                            currentVehicle.setType(xpp.getAttributeValue(null, "b"));
                            currentVehicle.setColor(xpp.getAttributeValue(null, "c"));
                        } else if (name.equals("x")) {
                            currentVehicle.appendToYears(xpp.getAttributeValue(null, "year"));
                        }
                    break;
                    case XmlPullParser.END_TAG:
                    name = xpp.getName();
                    if (name.equals("a")){
                            vehicles.add(currentVehicle);
                        }
                    break;
                }
                eventType = xpp.next();
            }


        } catch (FileNotFoundException e) {
            Log.e(TAG, "", e.fillInStackTrace());
        } catch (XmlPullParserException e) {
            Log.e(TAG, "", e.fillInStackTrace());
        } catch (IOException e) {
            Log.e(TAG, "", e.fillInStackTrace());
        }

        for(int i=0;i<vehicles.size();i++) {
            Vehicle vehicle = vehicles.get(i);
            Log.v(TAG, vehicle.toString());
        }

    }

    private class Vehicle {
        private String mType;
        private String mColor;
        private String mYears = "";

        void setType(String type) { mType = type; }
        String getType() { return mType; }
        void setColor(String color) { mColor = color; }
        String getColor() { return mColor; }
        void appendToYears(String year) {
            StringBuilder sb = new StringBuilder(mYears);

            if (!mYears.equals("")) {
                sb.append(", ");
            }
            sb.append(year);
            mYears = sb.toString();
        }
        String getYears() { return mYears; }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("[\"");
            sb.append(mType);
            sb.append("\", \"");
            sb.append(mColor);
            sb.append("\", \"");
                sb.append(mYears);
            sb.append("\"]");

            return sb.toString();
        }
    }
}

这只是为了让你走自己的路......