Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6860775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:27:03+00:00 2026-05-27T02:27:03+00:00

I’ve got this xml: <cars> <a b=car c=blue> <x year=01/> <x year=03/> </a> <a

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:27:04+00:00Added an answer on May 27, 2026 at 2:27 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
i want to parse a xhtml file and display in UITableView. what is the
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.