如何在android中解析XML并将其存储在类中

时间:2012-05-11 04:53:55

标签: java android xml xml-parsing android-xml

<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. 我想解析这个xml代码
  2. 我可以获得所有的值,但我有问题如何将它存储在类中并检索它并在listview中显示它..我可以得到标题,部分代码。但问题是如何获取多个节的子节点并将其存储在ArrayList中。
  3. 请帮我解析这个xml。我有代码,我正在工作,但它太乱了。但如果有人想看到我可以证明这一点。
  4. -

    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;
        }
    }
    }
    

3 个答案:

答案 0 :(得分:0)

使用SAX解析器;这是最好的选择。

答案 1 :(得分:0)

  

我想解析这个xml代码

=&GT;实施SAX Parser

  

我可以得到所有的值,但我有问题如何将它存储在课堂上并检索它并在listview中显示它..我可以获得标题,部分代码。但问题是如何获得多个节的子节点并将其存储在ArrayList中。

对于这个问题,我建议你创建一个带有属性title,subtitle等的类,为它创建Getter / setter方法。

为什么我建议使用此解决方案? 因为您正在尝试创建不同的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>();

相反,您可以创建单个ArrayList并每次添加对象。

在解析整个XML的最后,您将拥有包含多个对象的ArrayList,现在您可以使用此ArrayList为ListView创建适配器。

答案 2 :(得分:0)

我用这段代码来解析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);

希望这会对你有所帮助