0
<layout>

<menu1>
    <title>Oil Painting</title>
    <sectioncount>5</sectioncount>
    <cost>0</cost>
    <appcode>0</appcode>
    <section1.1>
        <title>Oil Painting basics</title>
        <subtitle>9 minutes 57 secs*!How to do Oil Painting</subtitle>
        <video1.1></video1.1>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section1.1>
    <section1.2>
        <title>Advacned Oil Painintg</title>
        <subtitle>6 minutes 23 secs*!Advanced Oil Painting</subtitle>
        <video1.2></video1.2>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section1.2>
    <section1.3>
        <title>Watercolors basic</title>
        <subtitle>9 minutes 51 secs*!How to use watercolors</subtitle>
        <video1.3></video1.3>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.3>
    <section1.4>
        <title>Watercolors Advanced</title>
        <subtitle>3 minutes 46 secs*!Advacned training- Watercolors</subtitle>
        <video1.4></video1.4>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.4>
    <section1.5>
        <title>Enamel Painting Basic</title>
        <subtitle>1 minute 56 secs*!Advacned training- Watercolors</subtitle>
        <video1.5></video1.5>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section1.5>     
</menu1>

<menu2>
    <title>Graphite Pencil Sketching</title>
    <sectioncount>4</sectioncount>
    <cost>.99</cost>
    <appcode>0</appcode>
    <section2.1>
        <title>Basic Graphite Pencil Sketching</title>
        <subtitle>5 minutes 29 secs*!What you need to know</subtitle>
        <video2.1></video2.1>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section2.1>
    <section2.2>
        <title>Advanced Graphite Pencil Sketching</title>
        <subtitle>3 minutes*!The proffessional way</subtitle>
        <video2.2></video2.2>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section2.2>
    <section2.3>
        <title>Graphite Pencil Tools</title>
        <subtitle>5 minutes*!what should you use</subtitle>
        <video2.3></video2.3>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section2.3>
    <section2.4>
        <title>Do's and Don't of Graphite Pencil Sketching</title>
        <subtitle>5 minutes*Cautions and precautions</subtitle>
        <video2.4></video2.4>
        <imgvideo>Landscape19.jpg</imgvideo>
        <description>video1.html</description>
    </section2.4>
</menu2>

<menu3>
    <title>Pen Sketching</title>
    <sectioncount>3</sectioncount>
    <cost>0</cost>
    <section3.1>
        <title>Basic Pen Sketching</title>
        <subtitle>2 minutes*!what you need to know</subtitle>
        <video3.1></video3.1>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.1>
    <section3.2>
        <title>Advanced Pen Sketching</title>
        <subtitle>3 minutes*!proffessional approach</subtitle>
        <video3.2></video3.2>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.2>
    <section3.3>
        <title>Pen Sketching Tools</title>
        <subtitle>3 minutes*!which pens to use</subtitle>
        <video3.3></video3.3>
        <imgvideo>fifa.jpg</imgvideo>
        <description>video1.html</description>
    </section3.3>
</menu3>
</layout>
  1. I want to parse this xml code
  2. I can get all the values but I am having problem how to store it in class and retrieve it and show it in listview.. I can get title , section code . but problem is how can I get child node of multiple section and store it in ArrayList.
  3. Please help me to parse this xml. I have code on which I'm working but its too messed up. but if anyone want to see that I can show that.

-

public class twitterFeedHandler extends DefaultHandler

StringBuilder sb = null;
String ret = "";
boolean bStore = false;
int howMany = 0;
Context context;
String a;
boolean config = false;
boolean menu = false;
Boolean section = false;

String elementValue = null;
Boolean elementOn = false;

private final static String TAG_Layout = "layout";
private final static String TAG_COnfig = "configuration";
private final static String TAG_Menu = "menu";

public static XMLGettersSetters data = null;
public static XMLGettersSettersMenu datamenu = null;

twitterFeedHandler() {
}


}

@Override
public void startDocument() throws SAXException {

}

@Override
public void endDocument() throws SAXException {

}

@Override
public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {


    try {





        elementOn = true;

        if (localName.equals(TAG_Layout)) {
            data = new XMLGettersSetters();
            datamenu = new XMLGettersSettersMenu();
        }if (localName.equals(TAG_COnfig)) {

            config = true;
            Log.i("Config localName", "configuration start");

        } else if (localName.equals(TAG_Menu)) {

            Log.i("Menu localName", "menu1 start");

            if (localName.equals("section1")) {
                if(localName.equals("stitle1"))
                Log.i("Section localName", "section start");
                section = true;

            }

            menu = true;


            Log.i("DATA --- > Menu ", datamenu.getCompany().toString());
        }

    } catch (Exception ee) {

        Log.d("error in startElement", ee.getStackTrace().toString());
    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {

    if (localName.equals("configuration")) {

        Log.i("Config localName", "configuration end");
        bStore = false;
    } else if (localName.equals("menu")) {
        Log.i("Menu localName", "menu end");

        if (localName.equals("section1")) {
            Log.i("Section localName", "section end");
            section = false;
        }
        menu = false;
    }
     if(localName.equals("title"))
         datamenu.setTitle(elementValue);
     if(localName.equals("sectioncount"))
         datamenu.setCompany_SectionCount(elementValue);
     if(localName.equals("section1"))
         if(localName.equals("stitle1"))
             datamenu.setSection(elementValue);

     Log.i("Menu localName214213132132132"," " + datamenu.getCompany_SectionCount() + " size " + datamenu.getSection());
    elementOn = false;

    }


@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (elementOn) {
        elementValue = new String(ch, start, length);
        Log.i("--------->Name", section + " " );

        if (config && menu == false)
            data.setCompany(elementValue);
        else 
            datamenu.setCompany(elementValue);

        elementOn = false;
    }

}

public class XMLGettersSetters {
    private ArrayList<String> company = new ArrayList<String>();

    public ArrayList<String> getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company.add(company);

    }
}

public class XMLGettersSettersMenu {
    private ArrayList<String> company = new ArrayList<String>();
    private ArrayList<String> title = new ArrayList<String>();
    private ArrayList<String> section_count = new ArrayList<String>();
    private ArrayList<String> section = new ArrayList<String>();


    public ArrayList<String> getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company.add(company);

    }

    public void setTitle(String company) {
        this.title.add(company);

    }

    public ArrayList<String> getTitle() {
        return title;
    }

    public void setCompany_SectionCount(String company) {
        this.section_count.add(company);

    }

    public ArrayList<String> getCompany_SectionCount() {
        return section_count;
    }

    public void setSection(String company) {
        this.section.add(company);

    }

    public ArrayList<String> getSection() {
        return section;
    }
}
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Zohaib Brohi
  • 576
  • 1
  • 7
  • 15
  • Go ahead and show us how you're trying to store the values in a class. – gobernador May 11 '12 at 04:55
  • 1
    see my answer as on http://stackoverflow.com/questions/2728064/parsing-local-xml-file-using-sax-in-android/8411959#8411959 either u check the ans of mine at http://stackoverflow.com/questions/10089291/how-to-parse-xml-in-android/10090154#10090154 – Khan May 11 '12 at 05:00
  • Parsing: duplicate of http://stackoverflow.com/q/4827344/741249 Saving data to a class: I would think this is basic programming skills. what exactly is it you are having problems with? Show in listview: plenty of examples on internet, e.g. http://www.vogella.com/articles/AndroidListView/article.html – THelper May 11 '12 at 08:35

3 Answers3

0

Use a SAX parser; it's the best option.

Anthony
  • 12,177
  • 9
  • 69
  • 105
P Srinivas Goud
  • 886
  • 1
  • 12
  • 30
0

i want to parse this xml code

=> Implement SAX Parser

i can get all the values but i am having problem how to store it in class and retrieve it and show it in listview.. i can get title , section code . but problem is how can i get child node of multiple section and store it in ArrayList.

For this problem, i would suggest you to create a class with the attributes title, subtitle and other, create Getter/setter methods for the same.

Why this solution i suggest? Because you are trying to create different ArrayList:

private ArrayList<String> company = new ArrayList<String>();
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> section_count = new ArrayList<String>();
private ArrayList<String> section = new ArrayList<String>();

Instead, you can create single ArrayList and add object everytime.

At the end of parsing whole XML, you will be having ArrayList with multiple objects, now you can use this ArrayList to create adapter for ListView.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

i used this code to parse xml

xml is like this

<point>
  <ImageFile name="hi"/>
</point>

NodeList image = doc.getElementsByTagName("Point");
            Node singleTerminalNode = image.item(i);
            Element firstLevel = (Element)singleTerminalNode;
            NodeList value1Nodes = (firstLevel).getElementsByTagName("ImageFile");
            imagess = ((Element)value1Nodes.item(imageindex)).getAttribute("name");
            System.out.println("image index issssssssss " + imageindex);

hope this will help you

Goofy
  • 6,098
  • 17
  • 90
  • 156