0

I've got this 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>

And I want to parse to an object (arraylist) like this:

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

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

Notice that the two year atrributes goes together in the same String. That's what I can not figure out.

The parser I'm using is the android native XMLPullParser

I cannot change XML format but I could use another android compatible parser if it's worth it.

If it's not clear it has to fit on an class like this:

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

the final result will as many arraylists(objects) as cars:

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

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

1 Answer 1

2

Your snippet is NOT XML… See Extensible Markup Language (XML) 1.1 (Second Edition).

  • c attribute values are not closed.
  • x tag are not closed.
  • You have multiple root…

If these are typos, you should correct that and I'll check if I can provide a real answer…

As a responsible programmer, don't use that format but rather migrate the data to a well-defined format but not bother to use this kind of corrupted format on a client application.

Update

Here is a quick-and-dirty implementation (using my own 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();
        }
    }
}

That's just to put you on your own way…

Sign up to request clarification or add additional context in comments.

4 Comments

well is a snipett I'll edit, It was only the relevant part. Done.
Would you like a List or what for x? Did you already design the undelying POJO for Vehicle?
The quality of answers will depends on the quality of your question… Please try to provide all the useful and required information at first shot…
Please don't forget to validate my answer if my implementation is sufficient to begin with your real implementation…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.